ChrisW
ChrisW

Reputation: 5068

Force CMake to use static libraries

[Shamelessly cross-posted from the CMake help list]

I'm trying to create binaries as statically as possible. The fortran code I've got has got X11 and quadmath as dependencies, and I've come across a number of issues (maybe each of these issues should be in a different question?):

Using these, CMake attempts to build every program statically (as expected) - however, it fails because I don't have Xaw.a - I can't find out whether this actually should exist. I have installed the latest libxaw7-dev which I was expecting to fix it. One option would be to compile the X11 libraries myself, but I don't really want to do that...

results in

-- The Fortran compiler identification is unknown
-- Check for working Fortran compiler: /usr/bin/gcc
-- Check for working Fortran compiler: /usr/bin/gcc  -- broken
CMake Error at /usr/share/cmake-2.8/Modules/CMakeTestFortranCompiler.cmake:54 (message):
The Fortran compiler "/usr/bin/gcc" is not able to compile a simple test program.

However, as you might have noticed, I set the location of the libquadmath.a; when I build a program which doesn't use X11 but does use quadmath when I use

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")

then the program does compile successfully (running ldd reports 'not a dynamic executable') - does this mean that the bug has been fixed, or does it only work because I set the location in CMake?

Upvotes: 7

Views: 7694

Answers (2)

Juan
Juan

Reputation: 3776

I was having a similar problem. Turns out that cmake was implicitly linking against libgfortran and libquadmath. To fix this I put the following in my top level CMakeLists.txt:

unset(CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES)

I could then explicitly link again the libraries using:

SET_TARGET_PROPERTIES(main_f PROPERTIES LINKER_LANGUAGE "C"
  LINK_FLAGS
  "/usr/local/Cellar/gcc/7.1.0/lib/gcc/7/libgfortran.a 
  /usr/local/Cellar/gcc/7.1.0/lib/gcc/7/libquadmath.a -lm -lgcc"
)

The static version of libgfortran is necessary because the shared library also depends on libquadmath. The added "-lm" and "-lgcc" bring in the system dynamic versions of these libraries. On a mac system, you would want to use the full path to your libm.a as well.

Upvotes: 1

Leonardo
Leonardo

Reputation: 1844

I guess your questions are not that much related, I don't know the answer for all of them.

For your static linking problems, since you're using GCC, you can pass multiple -static and -dynamic flags to it:

set(CMAKE_EXE_LINKER_FLAGS "-static ${STATIC_LIBS} -dynamic ${EVERYTHING ELSE} -static ${MORE_STATIC_LIBS}")

I don't know why Xaw.a isn't available on your system, probably because the package maintainer of your Linux distribution didn't really make them available.

Also, compiling everything static might make things not compatible between all distros out there and you cripple the ability for others to use improved, up-to-date libraries with your program, it might not be what you want.

If you intend to make a self-contained package of your program, it might be better just to include the shared libraries you used together, like Dropbox and many other proprietary applications do (Humble Bundle games are other example).

Upvotes: 0

Related Questions