1

I am trying to compile a C++ project that relies on the GSL library under a Windows host with the MSYS2 gcc toolchain.

The commands used to compile and link is as follows:

g++  -I/mingw64/include -Wall -c -o obj/main.o src/main.cpp
g++  -I/mingw64/include -Wall -c -o obj/physics.o src/physics.cpp
g++ -L/mingw64/lib -lgsl -lgslcblas -lm -o main.exe obj/main.o obj/physics.o

The flags were obtained like so:

$ gsl-config --libs
-L/mingw64/lib -lgsl -lgslcblas -lm

$ gsl-config --cflags
-I/mingw64/include

Which results in multiple undefined reference link time failures:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/physics.o:physics.cpp:(.text+0x5ff): undefined reference to `gsl_odeiv2_driver_apply'

Even though the library exists and contains the necessary function:

$ nm /mingw64/lib/libgsl.a | grep gsl_odeiv2_driver_apply
00000000000009c0 T gsl_odeiv2_driver_apply

I have tried building the GSL library under the same host with the same toolchain with:

./configure --prefix=/mingw64
make
make install

But the result is the same.

$ g++ --version
g++.exe (Rev1, Built by MSYS2 project) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Removing any reference to GSL in the code will of course allow compiling correctly. The project includes calls to the <windows.h> API.

Any ideas ?

gsimard
  • 643
  • 8
  • 24
  • 2
    you need to list your libraries after the object files that are using them – Alan Birtles Sep 30 '22 at 15:20
  • This was it. I never know the linker could be this picky about object file order ? Why ? – gsimard Sep 30 '22 at 15:28
  • 1
    it works through the objects/libraries from left to right collecting undefined symbols, any symbols that aren't used are discarded before moving on to the next object/library – Alan Birtles Sep 30 '22 at 15:30

0 Answers0