简体   繁体   中英

GCC: undefined reference to xxx

I know that have been already asked a lot of time but I can't really solve it... so I have a src folder where my main.c source is, an srclib where my lib.c file is stored and an include directory where my lib.h file is stored. now the makefile compiles the lib correctly and put the lib.a file in lib folder. the main.c includes the lib like this:

#include "lib.h"

and it's compiled with -I ../include option, but when i compile it I get the undefined reference to xxx error for every function in the library

so what am I missing?

Nope. -I is for including the header files. You also need to link with the library using -l option.

Note: You may need to provide the path-to-library using -L option.

To quote the online gcc manual

-llibrary

Search the library named library when linking...... The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a . The linker then uses this file as if it had been specified precisely by name.


EDIT:

To quote the remaining part of the same manual

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o . If bar.o refers to functions in z , those functions may not be loaded.

So, you need to put the -l<libanme> at the last of your compilation statement, as s_echo.c uses functions defined in that particular library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM