#Functions from header file are not called.

27 messages · Page 1 of 1 (latest)

twilit scaffold
#

The goal is just compile that code. There is no chance with "wrong path" to header file. IDE is able easily to find header file.
Code example:

#include <fstream>
#include <vector>
#include <iostream>
#include "./sample/UniLib.h"

using namespace std;

void sleepcp(int milliseconds);

void sleepcp(int milliseconds) // Cross-platform sleep function
{
#ifdef WIN32
    Sleep(milliseconds);
#else
    usleep(milliseconds * 1000);
#endif // win32
}

int main(int argc, char **argv) {
    printf("IP: %s\n", argv[1]);
    printf("Name: %s\n", argv[2]);

    void *ctx = create_context(argv[1], argv[2]);
    print_unilib_version();

    long int len;
    char *str;

    clearBuffer(&len, &str);
    printf("getPrinterConfiguration: %i\n", getPrinterConfiguration(ctx, &str, &len));
    sleepcp(10000);
}

function getPrinterConfiguration that is declared in UniLib.h produces the error like undefined reference to 'getPrinterConfiguration'. Generally, it looks like functions from UniLib.h are not used. Why it is possible?

worthy tartanBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

faint valley
#

UniLib.h only contains delcarations, not definitions. The declarations tell the compiler that the function exists, that part worked. Then the linker links the program together and needs the definition for the function. That part failed. There probably is some UniLib.a, UniLib.lib or UniLib.so you need to link in order to get the definition into the program.

twilit scaffold
leaden elm
#

What IDE are you using? How are you compiling currently? That might help people help you @twilit scaffold

faint valley
#

It might also be a UniLib.cpp.

#

You should know where you got the UniLib from and what files it came with.

#

How to link things depends on what build system you use. In the case of makefiles it depends on the compiler. Let's assume you're using gcc. The command at the end is likely g++ -o myprogram main.o other.o -L/path/to/UniLib -lUniLib.

#

g++ invokes gcc with the C++ settings, -o myprogram tells it to put the result in a file called myprogram, main.o and other.o are files you compiled using a makefile rule like

%.o: %.cpp 
   g++ -c $< -o $@
#

(this rule is subtly incorrect and will cause you massive pain later on, but makefiles being broken is standard)

twilit scaffold
faint valley
#

The -L/path tells the linker to add said path to search for libraries and -LUniLib tells it to search for a number of files with all sorts of endings.

twilit scaffold
faint valley
#

It's present in a compiled form. Maybe someone did g++ UniLib.cpp -c -o UniLib.so and then distributed the UniLib.so.

faint valley
#

-lUniLib should do it.

#

Otherwise just adding UniLib.so to your list of object files should also work.

twilit scaffold
faint valley
#

No, this mixes compiling with linking and also you need to use g++.

#
set -e
g++ -c main.cpp -o main.o
g++ main.o UniLib.so -o myprogram
./myprogram
#

Something like this I think.

twilit scaffold
faint valley
#

I don't think so. The linker will assume ./sample/Sample to be an executable.

#

Just compile everything first and then link everything together in 1 step.

#

There is a way to tell the linker to link .o files together into another .o file, but I don't know how.

twilit scaffold
#

!solved