Static linking depencies and position

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