Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

g++ "/ld.exe: cannot find l:mylib.a: No such file or directory

After I have read several forum posts to my problem which not helped me at all, I ask you for help.
I am trying to create a .dll with GNU compiler on Windows. This .dll needs a library, we call it mylib for now, to work. So i create a static library mylib.a with:

ar rcs mylib.a file1.o file2.o file3.o file4.o 

I created the .o files individually with

g++ -c file1.cpp.

Now to create the .dll and link mylib.a I enter the following command:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

g++ -shared -o mydll.dll Dynamicfunc.cpp -l:mylib.a

where Dynamicfunc.cpp contains the implementation of the .dll.
When executing this command, the following error message appears:

C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -l:mylib.a: No such file or directory
collect2.exe: error: ld returned 1 exit status

In most posts on this topic, the solution to the problem was to add the prefix "lib". So in my case -l:libmylib.a which unfortunately did not work either. Furthermore the mylib.a file is in the same directory as Dynamicfunc.cpp which makes using -L not necessary but even with -L:C:……\src, ld.exe does not find the specified mylib.a file.
If i turn all the object files (file1.o file2.o…) into a .lib instead of a .a library it also doesn´t work.
Furthermore I have tried to create mylib.a without

ar rcs libout.a ... 

but with

g++ -static -o mylib.a file1.o file2.o ...

also without success.
I would really appreciate any suggestions.
g++ -v:

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/13.2.0/lto-wrapper.exe
OFFLOAD_TARGET_NAMES=nvptx-none
Target: x86_64-w64-mingw32
Configured with: ../configure --prefix=/R/winlibs64ucrt_stage/inst_gcc-13.2.0/share/gcc --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --enable-offload-targets=nvptx-none --with-pkgversion='MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders'
--with-tune=generic --enable-checking=release --enable-threads=posix --disable-sjlj-exceptions --disable-libunwind-exceptions --disable-serial-configure --disable-bootstrap --enable-host-shared --enable-plugin --disable-default-ssp --disable-rpath --disable-libstdcxx-debug --disable-version-specific-runtime-libs 
--with-stabs --disable-symvers --enable-languages=c,c++,fortran,lto,objc,obj-c++ --disable-gold --disable-nls --disable-stage1-checking --disable-win32-registry --disable-multilib --enable-ld --enable-libquadmath --enable-libada --enable-libssp --enable-libstdcxx --enable-lto --enable-fully-dynamic-string --enable-libgomp 
--enable-graphite --enable-mingw-wildcard --enable-libstdcxx-time --enable-libstdcxx-pch --with-mpc=/d/Prog/winlibs64ucrt_stage/custombuilt --with-mpfr=/d/Prog/winlibs64ucrt_stage/custombuilt --with-gmp=/d/Prog/winlibs64ucrt_stage/custombuilt --with-isl=/d/Prog/winlibs64ucrt_stage/custombuilt 
--disable-libstdcxx-backtrace --enable-install-libiberty --enable-__cxa_atexit --without-included-gettext --with-diagnostics-color=auto --enable-clocale=generic --with-libiconv --with-system-zlib 
--with-build-sysroot=/R/winlibs64ucrt_stage/gcc-13.2.0/build_mingw/mingw-w64 CFLAGS='-I/d/Prog/winlibs64ucrt_stage/custombuilt/include/libdl-win32 -Wno-int-conversion  -march=nocona -msahf -mtune=generic -O2' CXXFLAGS='-Wno-int-conversion  -march=nocona -msahf -mtune=generic -O2' LDFLAGS='-pthread -Wl,--no-insert-timestamp -Wl,--dynamicbase -Wl,--high-entropy-va -Wl,--nxcompat -Wl,--tsaware'
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 13.2.0 (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders)

>Solution :

-l:mylib.a searches the library search path for a file named mylib.a. The current working directory is not in the library search path by default. You can add it there:

 gcc ... -L. -l:mylib.a

or just use the file name directly with no -l flag:

 gcc ... mylib.a

If you are creating a DLL, you might need another couple of flags:

 gcc -shared ... -Wl,-whole-archive mylib.a -Wl,-no-whole-archive

g++ -static -o mylib.a ... won’t work. Use ar to create a static library.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading