Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static build #28

Open
schmid786 opened this issue Dec 26, 2022 · 2 comments
Open

Static build #28

schmid786 opened this issue Dec 26, 2022 · 2 comments

Comments

@schmid786
Copy link

I'm trying to use nlopt-f in a statically compiled executable. But the build process of nlopt-f fails if BUILD_SHARED_LIBS=OFF is used.

# Building NLopt
cd nlopt
cmake -B build -S . -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=install
cmake --build build
cmake --build build -t install

# Building nlopt-f
cd ../nlopt-f
cmake -B build -S . -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=install -DNLopt_DIR=../nlopt/install/lib/cmake/nlopt
cmake --build build

Linker fails with undefined reference error. An excerpt of the log:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/<user>/nlopt/install/lib/libnlopt.a(local_optimizer.cc.obj):local_optimize:(.text+0xa3): undefined reference to `std::runtime_error::runtime_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/<user>/nlopt/install/lib/libnlopt.a(local_optimizer.cc.obj):local_optimize:(.text+0xb2): undefined reference to `operator delete(void*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/<user>/nlopt/install/lib/libnlopt.a(local_optimizer.cc.obj):local_optimize:(.text+0xb9): undefined reference to `std::runtime_error::~runtime_error()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/<user>/nlopt/install/lib/libnlopt.a(local_optimizer.cc.obj):local_optimize:(.text+0xc8): undefined reference to `__cxa_throw'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/<user>/nlopt/install/lib/libnlopt.a(local_optimizer.cc.obj):local_optimize:(.text+0xd3): undefined reference to `__cxa_free_exception'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/<user>/nlopt/install/lib/libnlopt.a(local_optimizer.cc.obj):local_optimize:(.text+0xed): undefined reference to `operator delete(void*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/<user>/nlopt/install/lib/libnlopt.a(local_optimizer.cc.obj):local_optimize:(.text+0x42b): undefined reference to `operator new(unsigned long long)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/<user>/nlopt/install/lib/libnlopt.a(local_optimizer.cc.obj):local_optimize:(.text+0x45c): undefined reference to `operator delete(void*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/<user>/nlopt/install/lib/libnlopt.a(local_optimizer.cc.obj):local_optimize:(.text+0xcf0): undefined reference to `std::__throw_length_error(char const*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/<user>/nlopt/install/lib/libnlopt.a(local_optimizer.cc.obj):local_optimize:(.xdata+0x10): undefined reference to `__gxx_personality_seh0'
collect2.exe: error: ld returned 1 exit status

If I build also the shared libraries of NLopt the build process of nlopt-f works but my executable has a dependency to the shared library of NLopt.

@schmid786
Copy link
Author

I got it running. I don't know why, but for the static build it is necessary that the linking step is conducted by the c++ compiler instead of the fortran compiler. This can be achived with cmake by setting:

enable_language(CXX)
set_property(TARGET ${prog} PROPERTY LINKER_LANGUAGE CXX)

Maybe this should be implemented in nlopt-f.

@awvwgk
Copy link
Member

awvwgk commented Dec 28, 2022

If you link static libraries their link dependencies get promoted, in this case there is an indirect dependency on the C++ runtime from NLOpt itself. Defaulting to C++ for linking will probably not work in all cases, instead a new link library could be added (in fact it should be handled by NLOpt's CMake export).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants