Stefan Falk
Stefan Falk

Reputation: 25387

g++ undefined reference though all files included

I have the problem that as the g++ tries to link the object files I receive the following error:

11:29:13 **** Build of configuration Debug for project daytime ****
make all 
'Building target: daytime'
'Invoking: Cross G++ Linker'
g++  -o "daytime"  ./tcf/services/daytime.o  ./tcf/main/main.o   
./tcf/services/daytime.o: In function `command_get_time_of_day':
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:38: undefined reference to `json_read_string'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:40: undefined reference to `exception'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:43: undefined reference to `exception'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:52: undefined reference to `write_stringz'
makefile:46: recipe for target 'daytime' failed
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:54: undefined reference to `write_stringz'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:56: undefined reference to `write_errno'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:58: undefined reference to `json_write_string'
./tcf/services/daytime.o: In function `ini_daytime_service':
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:70: undefined reference to `add_command_handler'
collect2.exe: error: ld returned 1 exit status
make: *** [daytime] Error 1

I have no idea why this is the case since e.g. #include <tcf/framework/json.h>is included and found.

Didn't gcc compile the corresponding *.c files such that this linker error occurs? What is the problem here?

Thank you.

Upvotes: 0

Views: 599

Answers (1)

codeling
codeling

Reputation: 11379

It is not enough to include the header files; you also have to specify the libraries where those functions are defined.

To make the linker find all those methods/classes (json_read_string, write_stringz, exception) you need to reference the library. If e.g. they are contained in a library called libjson.so, you should do:

g++ -ljson -o "daytime"  ./tcf/services/daytime.o  ./tcf/main/main.o

(or add the library to the project options, if eclipse is managing your make files).

Or if it's another .o file, include that in the compilation (-> or in the project, if eclipse is creating the make file).

Upvotes: 1

Related Questions