0

I am trying to compile a simple project which uses one of my headers. I am on Windows and I am using MinGW-W64-builds-4.3.5 Suppose the project is called test.cpp and I want to compile it using my headerosmanip which requires also the linking of its created static library libosmanip.lib. The static library has been compiled and created in MSYS2 and then copied into Windows filesystem. If I try to compile with:

g++ -std=c++17 -losmanip .\test.cpp

To search for the system headers and library path I did:

g++ -v test.cpp

and decided to put headers into C:\MinGW\bin\..\lib\gcc\i686-w64-mingw32\8.1.0\include\c++ and the static library into C:\MinGW\lib\gcc\i686-w64-mingw32\8.1.0\.

I got the following error:

C:/MinGW/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/bin/ld.exe: 
cannot find -losmanip
collect2.exe: error: ld returned 1 exit status

I've tried also by adding the -L option, linking the library path, or editing the LIBRARY_PATH variable ( $Env:LIBRARY_PATH+=";C:/MinGW/bin/../lib/gcc/i686-w64-mingw32/8.1.0/"), but it still doesn't work.

I tried to move the library into the same folder of the test.cpp file and compile, but again the same error occurs.

It's strange because I tried same thing on Ubuntu, MacOS, MSYS2 and Cygwin64 and it works.

Can you help, me please?

Gianluca Bianco
  • 656
  • 2
  • 11
  • 1
    Just to sort out... The compiler needs to know the header location for _compiling_, the library location _and_ names for _linking_. The header locations (directories) are given with `-I` (capital i). The library locations (directories) are given by `-L` (capital "ell"). The libraries are given by `-l` (non-capital "ell"). At least, on *ix, I'm used to leave out the prefix "lib" as well as the suffix - like you noted in your question: `libosmanip.a` → `-losmanip`. – Scheff's Cat Jun 22 '22 at 10:38
  • Sorry I wrongly said that I used `-I` for the lib path, but I was using `-L`, I've edited the question. However by adding both include and lib path with respectively `-I` and `-L` it doesn't work too. – Gianluca Bianco Jun 22 '22 at 10:42
  • Maybe, check how `libosmanip.lib` has been compiled. Not that you (accidentally) built it for x86 (aka. 32 bit). I'm not sure whether the compiler/linker would give a different error message or just ignore it in this case claiming not to find it (though I tend to the former)... – Scheff's Cat Jun 22 '22 at 10:44
  • @Scheff'sCat I compiled it using `MSYS2 MinGW x64` – Gianluca Bianco Jun 22 '22 at 10:45
  • My last stupid idea... Try `g++ -std=c++17 .\test.cpp -losmanip` (instead of `g++ -std=c++17 -losmanip .\test.cpp`). I somehow remember roughly that the order of libraries in command line was important somehow (when I still was making daily business on *ix OSes). I found: [Link order of libraries](https://www.linuxtopia.org/online_books/an_introduction_to_gcc/gccintro_18.html) – Scheff's Cat Jun 22 '22 at 11:00
  • @Scheff'sCat It doesn't work again :( – Gianluca Bianco Jun 22 '22 at 11:04
  • 1
    Here and there it is stated that static libraries should have the suffix `.a` (in MinGW), maybe, to emphasize that they are just object file archives, i.e. created with `ar`. I also read that MinGW supports `.lib` as well but on Windows, a `.lib` file can be a static library, or the counter-part of the `.dll` which is needed for linking the `.dll`. So, such a `.lib` could be either or... (You need a tool to find out what it really is, or if you have both, the smaller one is usually for the `.dll` as it contains only the trampoline symbols but not the full code). ;-) – Scheff's Cat Jun 22 '22 at 11:05
  • @Scheff'sCat same errors persist. I think that in Windows the suffix for static libraries is .lib – Gianluca Bianco Jun 22 '22 at 11:16
  • 1
    _I think that in Windows the suffix for static libraries is .lib_ That's not the complete truth: [DLL and LIB files - what and why?](https://stackoverflow.com/a/913744/7478597) – Scheff's Cat Jun 22 '22 at 12:01
  • @Scheff'sCat thanks, didn't know. However the error persists. – Gianluca Bianco Jun 22 '22 at 12:08

1 Answers1

1

I finally solved the issue. The problem was related to the fact the the suffix of my library was .lib. By changing it in .a and rebuilding the library passing the correct static library name to the ar command the problem disappeared.

Gianluca Bianco
  • 656
  • 2
  • 11
  • @Scheff'sCat i rebuilded the library specifying that the suffix of the static library must be the correct one when creating it with `ar` command. I edited the answer. – Gianluca Bianco Jun 23 '22 at 10:55
  • 1
    MinGW is an attempt to somehow mix the Windows and *ix worlds borrowing from both. That makes not-quite-easy things even more confusing. That said, I must admit that I'm doing daily business on Windows but won't like to miss the git (based on MinGW64) with git-bash and other stuff (which looks and feels like on Linux)... ;-) – Scheff's Cat Jun 23 '22 at 11:18