Gcc Error Undefined Reference To
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 more about Stack Overflow the company Business Learn undefined reference to in c more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags gcc undefined reference to function Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, undefined reference to g++ helping each other. Join them; it only takes a minute: Sign up C error: undefined reference to function, but it IS defined up vote 23 down vote favorite 8 Just a simple program, but I keep getting gcc undefined reference to std this compiler error. I'm using MinGW for the compiler. Here's the header file, point.h: //type for a Cartesian point typedef struct { double x; double y; } Point; Point create(double x, double y); Point midpoint(Point p, Point q); And here's point.c: //This is the implementation of the point type #include "point.h" int main() { return 0; } Point create(double x, double y) { Point p; p.x = x; p.y = y; return p; } Point midpoint(Point
How To Solve Undefined Reference Error In C
p, Point q) { Point mid; mid.x = (p.x + q.x) / 2; mid.y = (p.y + q.y) / 2; return mid; } And here's where the compiler issue comes in. I keep getting: testpoint.c: undefined reference to 'create(double x, double y)' While it is defined in point.c. This is a separate file called testpoint.c: #include "point.h" #include
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 Overflow the company
Undefined Reference C++
Business Learn more about hiring developers or posting ads with us Stack Overflow Questions undefined reference to a function c++ Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million c undefined reference to function in header file programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why am I getting a gcc “undefined reference” error trying to create shared objects? up vote 15 down vote favorite 7 http://stackoverflow.com/questions/5559250/c-error-undefined-reference-to-function-but-it-is-defined Why am I getting an "undefined reference" error using gcc? I am trying to create a shared object (.so) that exports one function, "external()". I then try to link against the .so but get "undefined reference 'external'". What am I doing wrong here? File: external.c int external() { return 5; } File: program.c int external(); int main(char** argv, int* argc) { return external(); } Commands: $ gcc -fPIC -c external.c $ gcc -shared -o http://stackoverflow.com/questions/8140494/why-am-i-getting-a-gcc-undefined-reference-error-trying-to-create-shared-objec libexternal.so external.o $ gcc -L. -lexternal -o program program.c /tmp/cc3MmhAE.o: In function `main': program.c:(.text+0x7): undefined reference to `external' collect2: ld returned 1 exit status I can even run nm and see that the .so is defining 'external': Command: $ nm libexternal.so | grep external 0000040c T external What am I missing here? c gcc ld shared-libraries share|improve this question edited Jul 7 '12 at 12:11 octopusgrabbus 5,57772882 asked Nov 15 '11 at 17:26 Warren 80311120 1 Does gcc -L. -o program program.c -lexternal fix it? –Flexo♦ Nov 15 '11 at 17:28 Ah, yes it does, thank you, but why? Why does -l have to be at the end? –Warren Nov 15 '11 at 17:35 @Warren, do you know of a gcc flag --echo-ld-options i.e. tell me what you're doing ? –denis Nov 20 '12 at 14:04 add a comment| 1 Answer 1 active oldest votes up vote 31 down vote accepted Recent versions of gcc/ld default to linking with --as-needed. This means if you write -lexternal before the C file the library will automatically get excluded (the order matters when testing if things are "needed" like this) You can fix this with either of: gcc -L. -o program program.c -lexternal gcc -L. -Wl,--no-as-needed -lexternal -o program program.c The latter of which passes --no-as-needed t
communities company blog Stack Exchange Inbox Reputation and Badges sign up log in tour help Tour Start 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 http://askubuntu.com/questions/194193/why-do-i-get-undefined-reference-errors-when-linking-against-openssl Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Ask Ubuntu Questions Tags Users Badges Unanswered Ask Question _ Ask Ubuntu is a http://eli.thegreenplace.net/2013/07/09/library-order-in-static-linking question and answer site for Ubuntu users and developers. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are undefined reference voted up and rise to the top Why do I get “undefined reference” errors when linking against OpenSSL? up vote 4 down vote favorite 1 My compilation process throws around errors like ..undefined reference to `BN_cmp' although I 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) / sizeof(src[0])); return dst[4]; } 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, 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 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 sym