aviad1
aviad1

Reputation: 354

GCC doesn't find functions in lib

I have a C file, which uses multiple lib files. I am trying to compile the file in the following way:

gcc -o myprogram main.c list.lib filelib.lib

However, when trying to compile I get a bunch of undefined reference errors of all the lib functions that I'm using.

I came accross a solution on the internet and tried the following:

gcc -o myprogram main.c -l list -l filelib

Now I get the following errors:

cannot find -llist
cannot fint -lfilelib

What am I doing wrong?

Edit: Both the libs were originally created using Visual Studio 2019, Release mode x64. I am using Windows 10, 64 bits architecture. In the folder I'm running gcc from I have the following files:

main.c
list.lib (copied from VS)
list.h (copied form VS)
filelib.lib (copied from VS)
filelib.h (copied from VS)

In my lib code in VS I made sure the functions have c-linkage:

#ifdef __cplusplus
#define C_LINKAGE extern "C"
#else
#define C_LINKAGE
#endif

(each declared function in both the libs starts with the C_LINKAGE macro)

Upvotes: 0

Views: 1066

Answers (3)

yo3hcv
yo3hcv

Reputation: 1669

I would like to give a BIG thanks to @Harkaitz for this hint.

I spent several days to figure out why GCC (arm cross compile on Windows) was not able to find my libs in group. I wish those spelling issues to be more documented somewhere...

Basically the ':' in between '-l' and 'lib.a' was solving the issue, like this:

-L./my_path_to_libs -Xlinker --start-group -l:libmain.a -Xlinker --end-group

Depending on arm gcc verzion, -Xlinker could be -Wl

Upvotes: 0

Harkaitz
Harkaitz

Reputation: 66

Just put "-l:liblist.lib" instead of "-llist" when the suffix is not ".a". That should solve the "Not found" issue at least.

Upvotes: 1

koder
koder

Reputation: 2093

The .lib files are MSVC specific, gcc can not handle them, gcc can handle .a libraries or dll's (on windows)

If you want to use gcc, rebuild the libraries with gcc, or let MSVC create DLL's.

Or stick to microsoft and use MSVC for everything.

Upvotes: 2

Related Questions