# Static linking depencies and position

**URL:** https://discourse.cmake.org/t/static-linking-depencies-and-position/6795
**Category:** Usage
**Created:** [November 4, 2022, 4:58pm UTC](https://discourse.cmake.org/t/static-linking-depencies-and-position/6795 "2022-11-04T16:58:41Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![lancethepants](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lancethepants/32/2898_2.png) [@lancethepants](https://discourse.cmake.org/u/lancethepants)
#### Post date: [November 4, 2022, 4:58pm UTC](https://discourse.cmake.org/t/static-linking-depencies-and-position/6795/1 "2022-11-04T16:58:41Z")

</div>

So I’ve come on the issue several times where I want to compile statically linked binaries in projects that use cmake. Sometimes a library dependency will have a dependency on another library. For example here is a cmake invocation to compile cmake for my environment.

cmake   
-Wno-dev   
-DCMAKE\_BUILD\_TYPE=Release   
-DCMAKE\_INSTALL\_PREFIX=/mmc   
-DCMAKE\_INCLUDE\_PATH=/mmc/include   
-DCMAKE\_LIBRARY\_PATH=/mmc/lib   
-DCMAKE\_C\_COMPILER=`which x86_64-linux-gcc`   
-DCMAKE\_CXX\_COMPILER=`which x86_64-linux-g++`   
-DCMAKE\_EXE\_LINKER\_FLAGS=“-s -static-pie -Wl,–start-group -lncursesw -lcurl -lssl -lcrypto -lz -Wl,–end-group”   
-Dpkgcfg\_lib\_NCURSES\_ncursesw=“/mmc/lib/libncursesw.a”   
-DCURSES\_CURSES\_LIBRARY=“/mmc/lib/libncursesw.a”   
-DCURSES\_EXTRA\_LIBRARY=“/mmc/lib/libtinfo.a”   
-DCURSES\_FORM\_LIBRARY=“/mmc/lib/libform.a”   
-DCURSES\_NCURSES\_LIBRARY=“/mmc/lib/libncursesw.a”   
-DCMAKE\_USE\_SYSTEM\_CURL=ON   
-DCURL\_LIBRARY\_RELEASE=“/mmc/lib/libcurl.a”   
-Dpkgcfg\_lib\_PC\_CURL\_curl=“/mmc/lib/libz.a”   
-DZLIB\_LIBRARY\_RELEASE=“/mmc/lib/libcurl.a”   
…

Here I want to compile cmake with libcurl, and the libcurl I’ve compiled has a dependency on libcrytpo/libssl and zlib. So in the linker flags I’ve added

“-Wl,–start-group -lncursesw -lcurl -lssl -lcrypto -lz -Wl,–end-group”

However when it gets to linking cmake puts -DCMAKE\_EXE\_LINKER\_FLAGS near the beginning and I get a lot of " undefined reference to" errors. Static linking is pickier than dynamic linking and it wants the libraries to come after the object files

/mmc/bin/x86\_64-linux-g++ -O3 -DNDEBUG -s -static-pie -Wl,–start-group -lncursesw -lcurl -lssl -lcrypto -lz -Wl,–end-group Source/CMakeFiles/cmake.dir/cmakemain.cxx.o Source/CMakeFiles/cmake.dir/cmcmd.cxx.o -o bin/cmake Source/libCMakeLib.a Source/kwsys/libcmsys.a Utilities/std/libcmstd.a Utilities/cmexpat/libcmexpat.a Utilities/cmlibarchive/libarchive/libcmlibarchive.a /mmc/lib/libcurl.a Utilities/cmliblzma/libcmliblzma.a Utilities/cmzstd/libcmzstd.a Utilities/cmbzip2/libcmbzip2.a Utilities/cmjsoncpp/libcmjsoncpp.a Utilities/cmlibuv/libcmlibuv.a -ldl -lrt Utilities/cmlibrhash/libcmlibrhash.a

When I move

“-Wl,–start-group -lncursesw -lcurl -lssl -lcrypto -lz -Wl,–end-group”

to the end of the command then it is able to successfully link.

How can I get the libraries to be put at the end when linking? I think cmake should have some ability to do this without the project creating a “-DDO\_STATIC” option.

thanks

Lance
