Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linker error on Linux: "undefined reference to"

I am able to make a shared library without problems. I create libcbitcoin.so (with no errors) and attempt to link against it with an executable as well as OpenSSL libraries. I use this command:

gcc -L/media/sf_BitEagle_Projects/cbitcoin/build/bin -lcbitcoin \
-Wl-rpath,/media/sf_BitEagle_Projects/cbitcoin/build/bin -lssl -lcrypto \
-L/usr/local/ssl/lib/ -o /media/sf_BitEagle_Projects/cbitcoin/build/bin/testCBAddress \
/media/sf_BitEagle_Projects/cbitcoin/build/obj/testCBAddress.o \
/media/sf_BitEagle_Projects/cbitcoin/build/obj/CBOpenSSLCrypto.o

The bin directory is the location of the library. The obj directory has the object files I wish to link into an executable. In the command I use the -L, -l and -rpath options which I thought was all that is needed for linking in linux. It seems I am wrong since I get errors like:

/media/sf_BitEagle_Projects/cbitcoin/test/testCBAddress.c:40:
undefined reference to `CBNewByteArrayFromString'

CBNewByteArrayFromString is found in the library. For some reason it is not being linked. OpenSSL too:

/media/sf_BitEagle_Projects/cbitcoin/dependencies/crypto/CBOpenSSLCrypto.c:37:
undefined reference to `SHA1'

How do I get the linking to work?

GCC version: gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

On Linux Mint 13

Thank you.

like image 668
Matthew Mitchell Avatar asked Sep 05 '12 00:09

Matthew Mitchell


People also ask

How do you fix undefined reference error?

So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.

How do I fix linker error?

You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.

How do I find my linker error?

Instead, linker errors are usually problems with finding the definitions for functions, structs, classes, or global variables that were declared, but never actually defined, in a source code file. Generally, these errors will be of the form "could not find definition for X".

What is a linker error?

Linker errors occur when the linker is trying to put all the pieces of a program together to create an executable, and one or more pieces are missing. Typically, this can happen when an object file or libraries can't be found by the linker. Fixing linker errors can be tricky.


2 Answers

Put the libraries after the object files on the link command line:

gcc /media/sf_BitEagle_Projects/cbitcoin/build/obj/testCBAddress.o \
    /media/sf_BitEagle_Projects/cbitcoin/build/obj/CBOpenSSLCrypto.o \
    -L/media/sf_BitEagle_Projects/cbitcoin/build/bin \
    -lcbitcoin -Wl-rpath,/media/sf_BitEagle_Projects/cbitcoin/build/bin \
    -L/usr/local/ssl/lib/ -lssl -lcrypto \
    -o /media/sf_BitEagle_Projects/cbitcoin/build/bin/testCBAddress

If you don't do that, the linker may decide that it needs nothing from a particular library at the stage of the link where it scans the library, and then it won't rescan the library later after it finds some undefined symbols in the object files. If you put the object files first, you don't run into this problem.

like image 163
Jonathan Leffler Avatar answered Oct 09 '22 11:10

Jonathan Leffler


I think it caused by can NOT find symbol, gcc will first go through from left, try to put the lib file at the end

like image 34
How Chen Avatar answered Oct 09 '22 10:10

How Chen