Gcc Linking Error Undefined Reference To
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions linking error undefined reference to function you might have Meta Discuss the workings and policies of linker error undefined reference to this site About Us Learn more about Stack Overflow the company Business Learn more about hiring
Gcc Undefined Reference To Function
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is
Undefined Reference To C
a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Linker error on Linux: “undefined reference to” up vote 10 down vote favorite 1 I am able to make a shared library without problems. I create libcbitcoin.so (with no errors) and attempt undefined reference c++ 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. linux gcc build linker share|improve this question edited Sep 5 '12 at 0:19 choroba 106k1089146 asked Sep 5 '12 at 0:01 Matthew Mitchell 1,48
Today's Posts Advanced Search Find the answer to your Linux question: Entire Site Articles Downloads Forums Linux Hosting Forum GNU Linux Zone Programming / Scripting Can't solve gcc linking error, please help. If this is your
G++ Undefined Reference To
first visit, be sure to check out the FAQ by clicking the link above. You ld undefined reference to function may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that linking undefined reference to you want to visit from the selection below. ** If you are logged in, most ads will not be displayed. ** Linuxforums now supports the Tapatalk app for your mobile device. Results 1 to 10 of 10 http://stackoverflow.com/questions/12272864/linker-error-on-linux-undefined-reference-to Thread: Can't solve gcc linking error, please help. Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode Enjoy an ad free experience by logging in. Not a member yet? Register. 06-23-2014 #1 b2rdark View Profile View Forum Posts Private Message View Articles Just Joined! Join Date Jun 2014 Posts 4 Can't solve gcc linking error, please help. Hi! I'm trying http://www.linuxforums.org/forum/programming-scripting/201891-cant-solve-gcc-linking-error-please-help.html to compile a very simple code which uses functions defined in the Linux PCI Utilities library, but the linker stops with "undefined reference to" errors and im running out of ideas. I'm on ubuntu 14.04 LTS (64-bit) and i've installed the libpci-dev (1:3.2.1-1ubuntu5) package too. Here is my test.c: Code: #include
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) / http://eli.thegreenplace.net/2013/07/09/library-order-in-static-linking sizeof(src[0])); return dst[4]; } It runs just fine and the return value is 5. https://latedev.wordpress.com/2014/04/22/common-c-error-messages-2-unresolved-reference/ 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]; } } undefined reference If the previous snippet gets linked with this library, 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 undefined reference to well as how to avoid the problem (and more serious problems, like circular 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
error message (or “unresolved external symbol, for Visual C++ users). This is not actually a message from the compiler, but is emitted by the linker, so the first thing to do is to understand what the linker is, and what it does. Linker 101 To understand the linker, you have to understand how C++ programs are built. For all but the very simplest programs, the program is composed of multiple C++ source files (also known as “translation units”). These are compiled separately, using the C++ compiler, to produce object code files (files with a .o or a .obj extension) which contain machine code. Each object code file knows nothing about the others, so if you call a function from one object file that exists in another, the compiler cannot provide the address of the called function. This is where the the linker comes in. Once all the object files have been produced, the linker looks at them and works out what the final addresses of functions in the executable will be. It then patches up the addresses the compiler could not provide. It does the same for any libraries (.a and .lib files) you may be using. And finally it writes the executable file out to disk. The linker is normally a separate program from the compiler (for example, the GCC linker is called ld) but will normally be called for you when you use your compiler suite’s driver program (so the GCC driver g++ will call ld for you). Traditionally, linker technology has lagged behind compilers, mostly because it’s generally more fun to build a compiler than to build a linker. And linkers do not necessarily have access to the source code for the object files they are linking. Put together, you get a situation where linker errors, and the reasons for them, can be cryptic in the extreme. Undefined reference Put simply, the “undefined reference” error means you have a reference (nothing to do with the C++ reference type) to a name (function, variable, constant etc.) in your program that the linker cannot find a definition for when it looks through all the object files an