1

I am new to cmake so, sorry if this question is very basic.

I want to build my project as statically linked with each standard library it uses. Like, in gcc if we want to link the standard system libraries(libc, libgcc etc) as static, we specify '-static' option at the time of compilation.(Like gcc main.c -static -o main). How we can achieve the same in cmake?

I have read multiple threads to define a how to define a library, how to build, link(as static & shared). But that is all for a custom library, I need information for standard system libraries.

[Edit] In my project, I am using yocto at the top and the cmake is been used underneath it. running directly cmake works fine, as the generated executable has no dependencies on any shared library, all the used libraries are linked statically. But compiling from yocto causing the issue. the executable generated from the yocto build shows the dependencies on several standard shared libraries.

How we can specify static linking of standard libraries in yocto cmake?

Thanks in advance.

Dixit
  • 73
  • 10
  • 2
    Does this answer your question? [Compiling a static executable with CMake](https://stackoverflow.com/questions/24648357/compiling-a-static-executable-with-cmake) – kaylum May 04 '20 at 07:59
  • It does not. after trying all the answers of that thread, still the ldd command output on my generated binary shows below listed shared library to be used; linux-vdso.so.1 => (0x00007ffcc7548000) libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fb0b6cf1000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb0b6927000) libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb0b661e000) – Dixit May 04 '20 at 11:04
  • 3
    If the suggested answer did not work for you, you will need to show us what you tried to reveal what was wrong with it. – Mike Kinghan May 04 '20 at 13:21
  • Sure @MikeKinghan. As per one of the answer of that thread, adding below line should link all the standard libraries statically. SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a") SET(BUILD_SHARED_LIBS OFF) SET(CMAKE_EXE_LINKER_FLAGS "-static") I have added those line in my CMakeLists.txt but when I check dynamic dependencies of executable after compiling it still shows libc.so, libm.so, libstdg++.so as dynamic dependencies. If those libs were statically linked than it should not show in the ldd. – Dixit May 05 '20 at 05:07
  • I have created a sample cmake project to ensure this, By making these changes in that sample project, the output generated executable has no dependencies on standard library, that means the standard libraries are linked as static. In my project, I am using yocto at the top and the cmake is been used underneath it. But I think this should not cause such issue. Correct me if I am wrong. – Dixit May 05 '20 at 05:13
  • The solution; SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a") SET(BUILD_SHARED_LIBS OFF) SET(CMAKE_EXE_LINKER_FLAGS "-static") Doesn't seems to be working in my case. – Dixit May 05 '20 at 07:36
  • Static linking with `libstdc++` is achieved with `-static-libstdc++`. You may specify this option either in `target_link_libraries` command, as suggested by [that answer](https://stackoverflow.com/a/46811527/3440745), or with setting `CMAKE_EXE_LINKER_FLAGS` CMake variable. If this way doesn't work for your (or does work under pure CMake but does not work under yocto), then add your code into the question. Otherwise we cannot help you. See [ask]. – Tsyvarev May 05 '20 at 14:04
  • Be careful as cmake yocto class add a lot of default options [see here](http://git.yoctoproject.org/cgit/cgit.cgi/poky/tree/meta/classes/cmake.bbclass#n164), maybe you need to enable static lib generation see [here](https://www.yoctoproject.org/docs/current/mega-manual/mega-manual.html#migration-2.1-poky-reference-distribution-changes) with `DISABLE_STATIC = ""` – Nayfe May 05 '20 at 14:39

0 Answers0