Gcc Library Linking Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn
Gcc Linker Error Undefined Reference
more about Stack Overflow the company Business Learn more about hiring developers or posting undefined reference to function in shared library ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack
Gcc Linking Order
Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Trying to include a library, but keep getting 'undefined reference to' gcc undefined reference to function messages up vote 36 down vote favorite 18 I am attempting to use the libtommath library. I'm using the NetBeans IDE for my project on Ubuntu linux. I have downloaded and built the library, I have done a 'make install' to put the resulting .a file into /usr/lib/ and the .h files into /usr/include It appears to be finding the files appropriately (since I no longer get those ld undefined reference to function errors, which I did before installing into the /usr directories). However, when I create a simple main making a call to mp_init (which is in the library), I get the following error when I attempt to make my project: mkdir -p build/Debug/GNU-Linux-x86 rm -f build/Debug/GNU-Linux-x86/main.o.d gcc -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.c mkdir -p dist/Debug/GNU-Linux-x86 gcc -o dist/Debug/GNU-Linux-x86/cproj1 build/Debug/GNU-Linux-x86/main.o build/Debug/GNU-Linux-x86/main.o: In function 'main': /home/[[myusername]]/NetBeansProjects/CProj1/main.c:18: undefined reference to `mp_init' collect2: ld returned 1 exit status make[2]: *** [dist/Debug/GNU-Linux-x86/cproj1] Error 1 So, it looks like the linker can't find the function within the library, however it IS there, so I just don't know what could be causing this. Any help would be appreciated. I get the same error if I type the gcc command directly and skip the makefile, I also made sure the static library got compiled with gcc as well. Edited to Add: I get these same errors if I do the compile directly and add the library with -l or -L: $ gcc -l /usr/lib/libtommath.a main.c /usr/bin/ld: cannot find -l/usr/lib/libtommath.a collect2: ld returned 1 exit status $ gcc -llibtommath.a main.c /usr/bin/ld: cannot find -llibtommath.a collect2: ld returned 1 exit status $ gcc -Llibtommath.a main.c /tmp/ccOxzclw.o: In function `main': main.c:(.text+0x18): undefine
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack
Linking Undefined Reference To
Overflow the company Business Learn more about hiring developers or posting ads with us undefined reference gcc Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a
Linker Error Undefined Reference To
community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why does the order in which libraries are linked sometimes cause errors in GCC? up http://stackoverflow.com/questions/1517138/trying-to-include-a-library-but-keep-getting-undefined-reference-to-messages vote 263 down vote favorite 171 Why does the order in which libraries are linked sometimes cause errors in GCC? c++ gcc linker share|improve this question edited Jun 9 '15 at 18:48 Ciro Santilli 烏坎事件2016六四事件 法轮功 52.4k10225166 asked Sep 5 '08 at 2:24 Landon 5,538122928 7 you might want to change your accepted answer, as per ** user9876**'s comment below. –Vorac Feb 22 '13 at 7:24 See also http://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc now stackoverflow.com/questions/7826448/… -- TLDR gcc changed to more-strict behavior (relatively) recently. –tripleee Jan 5 '15 at 6:17 add a comment| 10 Answers 10 active oldest votes up vote 365 down vote (See the history on this answer to get the more elaborate text, but I now think it's easier for the reader to see real command lines). Common files shared by all below commands $ cat a.cpp extern int a; int main() { return a; } $ cat b.cpp extern int b; int a = b; $ cat d.cpp int b; Linking to static libraries $ g++ -c b.cpp -o b.o $ ar cr libb.a b.o $ g++ -c d.cpp -o d.o $ ar cr libd.a d.o $ g++ -L. -ld -lb a.cpp # wrong order $ g++ -L. -lb -ld a.cpp # wrong order $ g++ a.cpp -L. -ld -lb # wrong order $ g++ a.cpp -L. -lb -ld # right order The linker searches from left to right, and notes unresolved symbols as it go. Only a library resolves the symbol, it takes the object files of that library to resolve the symbol (b.o out of libb.a in this case). Dependencies of static libraries against each other work the same - the library that needs symbols must be fi
volatile char src[] = {1, 2, 3, 4, 5}; volatile char dst[50] = {0}; void* memcpy(void* dst, void* src, int len); int main(int argc, const char* argv[]) { memcpy(dst, src, sizeof(src) / sizeof(src[0])); return dst[4]; } http://eli.thegreenplace.net/2013/07/09/library-order-in-static-linking It runs just fine and the return value is 5. Now, suppose this is part of a larger project that consists of many object files and libraries, and somewhere within the project there's a library that contains this code: void memcpy(char* aa, char* bb, char* cc) { int i; for (i = 0; i < 100; ++i) { cc[i] = aa[i] + bb[i]; } } If the previous snippet gets linked with this library, undefined reference what happens? Would you expect it to still return 5? Return something else? Crash? The answer is: it depends - the result can be either correct or a segmentation fault. It depends on the order in which the objects and libraries in the project were fed to the linker. If you fully understand why this depends on linking order, as well as how to avoid the problem (and more serious problems, like circular undefined reference to dependencies) then congratulate yourself and move on - this article is probably not for you. Otherwise, read on. The basics Let's start by defining the scope of this article: first, my examples are demonstrating the use of the gcc and binutils toolchain on Linux. Compatible toolchains (like clang instead of gcc) apply too. Second, the discussion here resolves around static linking that's done at compile/link time. To understand why linking order matters, it's first instructional to understand how the linker works with respect to linking libraries and objects together. Just as a quick reminder - an object file both provides (exports) external symbols to other objects and libraries, and expects (imports) symbols from other objects and libraries. For example, in this C code: int imported(int); static int internal(int x) { return x * 2; } int exported(int x) { return imported(x) * internal(x); } The names of the functions speak for themselves. Let's compile it and look at the symbol table: $ gcc -c x.c $ nm x.o 000000000000000e T exported U imported 0000000000000000 t internal This means: exported is an external symbol - defined in the object file and visible from the outside. imported is an undefined symbol; in other words, the linker is expected to find it elsewhere. When we talk about linking later, the term