Home > Net >  Linker failing to find existing static library with correct name
Linker failing to find existing static library with correct name

Time:03-17

I am currently writing a small test application to explore statically linking libraries and accessing their symbols at runtime with Boost.DLL. I am attempting to compile a very simple static library and link it with a very simple application, using MinGW-w64, and am doing this with a Makefile and mingw32-make.

The following is the Makefile:

B_INCLUDE_PATH = -IC:/Unix/boost_1_78_0/

INCLUDE_PATH = -IC:/Unix/boost_1_78_0/
LIB_PATH = -LC:/Unix/boost_1_78_0/stage/lib -L.
LIBS = -llibboost_filesystem-mgw8-mt-x64-1_78 -llibboost_system-mgw8-mt-x64-1_78
SRCS = main.cpp A.cpp
OUT = out

#build executable
out:
    g   -c -Wall ${B_INCLUDE_PATH} B.cpp -o B.o
    ar -rcs -o libB.lib B.o
    g   -Wall ${INCLUDE_PATH} ${LIB_PATH} ${SRCS} ${LIBS} -Wl,-Bstatic -lB -o ${OUT}

#build and run executable
run: out
    ./out

The following is the command prompt output:

C:\Unix\VSC\Derive_Test>mingw32-make
g   -c -Wall -IC:/Unix/boost_1_78_0/ B.cpp -o B.o
ar -rcs -o libB.lib B.o
g   -Wall -IC:/Unix/boost_1_78_0/ -LC:/Unix/boost_1_78_0/stage/lib -L. main.cpp A.cpp -llibboost_filesystem-mgw8-mt-x64-1_78 -llibboost_system-mgw8-mt-x64-1_78 -Wl,-Bstatic -lB -o out
C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lB
collect2.exe: error: ld returned 1 exit status
mingw32-make: *** [Makefile:13: out] Error 1

I have tried renaming the output file from ar with and without the prefix lib, and with the suffixes .a, .lib, and .so, with no success.

Thank you in advance for any help.

CodePudding user response:

Replace the command calls to g (last line in out target) with this:

g   -Wall ${INCLUDE_PATH} ${LIB_PATH} ${SRCS} ${LIBS} -c -o main.o
g   B.o main.o -o ${OUT}

CodePudding user response:

I got it to compile; it seemed to be that ld on MinGW-w64 was looking for the specific name lib<library name>.a. I must have neglected this specific combination of prefix and suffix. Thanks for the responses.

  • Related