g++ undefined reference though all files included - c++

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.

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).

Related

Undefined Reference to `SDL2_Init` with LD when G++ is not giving an error

(FYI Yes I do know that a similar question exists, But I have already tried all the answers.)
I'm trying to setup SDL2 with MinGW and I have everything set up beside one thing. Whenever I try to compile G++ gives no errors but then LD gives me a: undefined reference to SDL_Init Error.
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\AppData\Local\Temp\cc28dE2R.o: in function `SDL_main':
C:\Users\user\Documents\SDL2/src/main.cpp:11: undefined reference to `SDL_Init'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\Documents\SDL2/src/main.cpp:14: undefined reference to `SDL_GetError'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\Documents\SDL2/src/main.cpp:18: undefined reference to `IMG_Init'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\Documents\SDL2/src/main.cpp:20: undefined reference to `SDL_GetError'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(main.o):(.text.startup+0xc0): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
The terminal process "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command C:\MinGW\bin\g++.exe -g src\*.cpp -o build\game.exe -ID:C:\MinGW\include -LD:C:\MinGW\lib -mwindows -lmingw32 -lSDL2main -lSDL2_image -lSDL2" terminated with exit code: 1.
I have tried to move the args around. doing -mwindows, adding -lmingw32. I still keep getting the same errors.. I was able to fix the WinMain Error.. But everything else I couldn't fix.
The command I'm Running to compile:
C:\MinGW\bin\g++.exe -g src\*.cpp -o build\game.exe -ID:C:\MinGW\include -LD:C:\MinGW\lib -mwindows -lSDL2main -lSDL2_image -lSDL2
If i could get any help that would be amazing
The common cause of mysterious undefined reference errors on MinGW is using libraries compiled for x64 with an x32 compiler, or vice versa. SDL ships both x32 and x64 ones, try the other ones.
I was able to fix the WinMain Error
Be aware that the intended way of fixing undefined reference to WinMain#16 is not adding #define SDL_MAIN_HANDLED and not doing #undef main. Once you get the right libraries (see the first paragraph), the error should disappear by itself.
Make sure you use int main(int, char**) (and not int main() nor void main()), otherwise it won't work.
-ID:C:\MinGW\include -LD:C:\MinGW\lib
You shouldn't need those flags. Those directories will be searched automatically.
Since you didn't specify any other directories, I assume you placed the SDL files directly into the compiler directories, which isn't a good practice, IMO.
I assume D:C: a typo?
-mwindows
The only purpose of this flag (that I know of) is to prevent your app from automatically opening a terminal window for itself, for release builds.

How to link libssh library in Makefile

I get following error, when i tried to compile my test program
g++ main.cpp -o main
/tmp/ccICBwKO.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `ssh_new'
main.cpp:(.text+0x2d): undefined reference to `ssh_free'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
In my make file i have:
all: main
$main: main.cpp
g++ main.cpp -o main -lssh
Thanks for help. (I really hate C/C++...)
As you can see from the log, make is not using the rule you have given for $main. I suspect that you want to name it either main or $(main).
$main concatenates the value of variable $m (defaults to nothing as m is not defined) with ain, so that you've created a rule for ain instead of main. make thus rely on its default rules for creating an executable from a .cpp file, which does not include linking with libssh.

Compiling Fortran netCDF programs on Ubuntu

Ok, newb question here.
I'm trying to compile simple_xy_wr.f90 -- a netCDF example program -- using gfortran on Ubuntu, and I must be doing something pretty silly; I don't have much experince compiling Fortran.
First, I've got the libnetcdf-dev package installed, which includes files like
/usr/lib/libnetcdf.a
/usr/lib/libnetcdff.a
/usr/include/netcdf.mod
So, I've tried to compile the code with (various command like)
f95 -o xy -I/usr/include/ -L/usr/lib/ -lnetcdff -lnetcdf simple_xy_wr.f90
and I get the following output
/tmp/ccE6g7sr.o: In function `check.1847':
simple_xy_wr.f90:(.text+0x72): undefined reference to `__netcdf_MOD_nf90_strerror'
/tmp/ccE6g7sr.o: In function `MAIN__':
simple_xy_wr.f90:(.text+0x284): undefined reference to `__netcdf_MOD_nf90_create'
simple_xy_wr.f90:(.text+0x2b6): undefined reference to `__netcdf_MOD_nf90_def_dim'
simple_xy_wr.f90:(.text+0x2e8): undefined reference to `__netcdf_MOD_nf90_def_dim'
simple_xy_wr.f90:(.text+0x432): undefined reference to `__netcdf_MOD_nf90_def_var_manydims'
simple_xy_wr.f90:(.text+0x468): undefined reference to `__netcdf_MOD_nf90_enddef'
simple_xy_wr.f90:(.text+0x4aa): undefined reference to `__netcdf_MOD_nf90_put_var_2d_fourbyteint'
simple_xy_wr.f90:(.text+0x4cb): undefined reference to `__netcdf_MOD_nf90_close'
collect2: error: ld returned 1 exit status
I think that I'm including the right libraries. E.g. it seems that __netcdf_MOD_nf90_strerror should be there:
$ nm /usr/lib/libnetcdff.a | grep __netcdf_MOD_nf90_strerror
000000000004a100 T __netcdf_MOD_nf90_strerror
What am I doing wrong?
(FWIW, a few relevant references I've looked at are below.
undefined reference using netcdf library
Compiling problems with gfortran and NETCDF
Compiling and Running Fortran Programs - a basic guide
)
Ordering of object files and archives on the linker command line is very important on Unix systems since the default linker behaviour is to search for symbol definitions only in archives that follow the object file or archive, where an unresolved reference was found, referred to single pass linking.
This means that if your code references __netcdf_MOD_nf90_strerror, then the archive that contains the definition of this symbol (libnetcdff.a) must appear after the list of object files from your program. libnetcdff.a itself references symbols from the C library libnetcdf.a, hence it must be linked after libnetcdff.a. So the correct link order is:
/tmp/ccE6g7sr.o libnetcdff.a libnetcdf.a
where /tmp/ccE6g7sr.o is the temporary object file that the assembler produces from the compiled source file. The correct command line to compile your code is then:
f95 -o xy -I/usr/include/ simple_xy_wr.f90 -lnetcdff -lnetcdf
In this case the linker is not called directly, rather the compiler does it. GCC compilers pass all link-related things in the same order to an intermediate utility called collect2 which then calls the actual linker ld.
Note that if shared object versions of the netCDF library archives are also present (i.e. there are libnetcdff.so and libnetcdf.so), then the linker would prefer them to the static archives (unless static linking is enabled with the -static option) and the final link phase would be handled to the run-time link editor (RTLD) (/lib64/ld-linux-x86-64.so.2 on Ubuntu). In this case the same command line as in your question would actually succeed without link errors, despite the fact that both libraries are positioned before the code that references them, as the missing symbol references would be resolved by the RTLD while it is loading the executable file.
In Ubuntu 12.10, the order of the libraries is the trick (as Hristo suggested):
angelv#palas:~$ gfortran -o xy -I/usr/include/ -L/usr/lib/ -lnetcdf -lnetcdff simple_xy_wr.f90
/tmp/ccj95anF.o: In function `check.1847':
simple_xy_wr.f90:(.text+0x72): undefined reference to `__netcdf_MOD_nf90_strerror'
/tmp/ccj95anF.o: In function `MAIN__':
simple_xy_wr.f90:(.text+0x284): undefined reference to `__netcdf_MOD_nf90_create'
simple_xy_wr.f90:(.text+0x2b6): undefined reference to `__netcdf_MOD_nf90_def_dim'
simple_xy_wr.f90:(.text+0x2e8): undefined reference to `__netcdf_MOD_nf90_def_dim'
simple_xy_wr.f90:(.text+0x432): undefined reference to `__netcdf_MOD_nf90_def_var_manydims'
simple_xy_wr.f90:(.text+0x468): undefined reference to `__netcdf_MOD_nf90_enddef'
simple_xy_wr.f90:(.text+0x4aa): undefined reference to `__netcdf_MOD_nf90_put_var_2d_fourbyteint'
simple_xy_wr.f90:(.text+0x4cb): undefined reference to `__netcdf_MOD_nf90_close'
collect2: error: ld returned 1 exit status
angelv#palas:~$ gfortran -o xy -I/usr/include/ simple_xy_wr.f90 -L/usr/lib/ -lnetcdf -lnetcdff
angelv#palas:~$ ./xy
0 12 24 36
*** SUCCESS writing example file simple_xy.nc!

with -lpthread, g++ compiler error, "undefined reference to " semaphore calls such as `sem_open'

I am new to posix thread library, and I tried to compile a sample code from a tutorial with:
g++ -lpthread agreement.cpp -o agreement
however I was not able to compile the code and got the following error message:
a3q2.cpp:(.text+0x7e): undefined reference to `sem_open'
a3q2.cpp:(.text+0xab): undefined reference to `sem_wait'
a3q2.cpp:(.text+0x290): undefined reference to `sem_post'
a3q2.cpp:(.text+0x2af): undefined reference to `sem_close'
a3q2.cpp:(.text+0x2bb): undefined reference to `sem_unlink'
collect2: ld returned 1 exit status
make: *** [a3q2_exe] Error 1
I am aware that -lpthread is needed for compilation to work, but is there any other options i might need to solve the problem? if not how do I have to install the "proper" pthread library?
Thanks for your help!
You want the compile option -pthread (if you are really using pthreads). If you just need those functions they are in librt so use -lrt

Test for external undefined references in Linux

Is there a built in linux utility that I can use to test a newly compiled shared library for external undefined references? Gcc seems to be intelligent enough to check for undefined symbols in my own binary, but if the symbol is a reference to another library gcc does not check at link time. Instead I only get the message when I try to link to my new library from another program.
It seems a little silly to get undefined reference messages in a library when I am compiling a different project so I want to know if I can do a check on all references internal and external when I build the library not when I link to it.
Example error:
make -C UnitTests debug
make[1]: Entering directory `~/projects/Foo/UnitTests`
g++ [ tons of objects ] -L../libbar/bin -lbar -o UnitTests
libbar.so: undefined reference to `DoSomethingFromAnotherLibrary`
collect2: ld returned 1 exit status
make[1]: *** [~/projects/Foo/UnitTests] Error 1
Usually, undefined references are allowed when linking shared objects, but you can make the linker generate an error if there are undefined symbols in the object files that you are linking to create the shared library by supplying -z defs to the linker (or equivalently -Wl,-z,defs in the gcc command that calls the linker).