0

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)

aviad1
  • 81
  • 5
  • 1
    What functions are you getting errors about? And where (in which libraries) are they defined? And what are the library dependencies? Which library depends on which other library (because order matter when you link with libraries). – Some programmer dude Dec 28 '20 at 11:19
  • Order does indeed matter, and if libraries depend on each other, you should add the `start-group` / `end-group`option. See https://stackoverflow.com/questions/5651869/what-are-the-start-group-and-end-group-command-line-options – Bktero Dec 28 '20 at 11:37
  • filelib depends on list, and main.c depends on both – aviad1 Dec 28 '20 at 11:50
  • I pretty much get an error for each and every lib function I try to use – aviad1 Dec 28 '20 at 11:51
  • Then you should change the order. If library `A` depends on library `B`, then `A` needs to come *first* on the command-line. – Some programmer dude Dec 28 '20 at 11:51
  • When using ```start-group``` and ```end-group``` I get the following error: ```gcc: error: unrecognized command line option '--start-group'; did you mean '--stdarg-opt'?``` – aviad1 Dec 28 '20 at 11:52
  • Tried doing the following: ```gcc -o myprogram -l filelib -l list main.c```. Still get the same ```cannot find``` error – aviad1 Dec 28 '20 at 11:54
  • Try without the `-l` options. If you use the `-l` option and the libraries are not in a "standard" location you need to tell the linker where the libraries are with the `-L` option. And the source file should be *first* since it depends on both libraries. I.e. `gcc -o myprogram main.c filelib.lib list.lib` (if both libraries are in the current directory). – Some programmer dude Dec 28 '20 at 11:57
  • Now I get all the undefined references again – aviad1 Dec 28 '20 at 12:11
  • 1
    Please add a directory listing **as text** that shows what files are present. What is your OS? Did you create the libraries yourself? If yes, show the commands you used to build the libraries. Please [edit] your question to add the requested information, don't use comments to answer. – Bodo Dec 28 '20 at 12:23

1 Answers1

2

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.

koder
  • 1,585
  • 2
  • 8