how can i build static program by cmake+clang on windows?

65 Views Asked by At

Sorry for my poor English first.

I'm trying to build a c++ program using cmake+clang on a Windows system.

My CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
set(CMAKE_C_COMPILER "C:/Software/LLVM/bin/clang.exe")
set(CMAKE_CXX_COMPILER "C:/Software/LLVM/bin/clang++.exe")

project(helloworld)

set(CMAKE_VERBOSE_MAKEFILE ON)

set(CMAKE_CXX_COMPILER_TARGET i686-pc-windows-msvc)

set(CMAKE_CXX_STANDARD 17)
set(SOURCE helloworld.cpp)
add_executable(${PROJECT_NAME} ${SOURCE})

and the command line:

cmake -G"NMake Makefiles" ..
nmake

It works well. But the program it compiles is not static, which means that when I release it, users need to install the runtime library. There is an /MT option in visual studio that can complete the static compilation process, but I haven't found it in CMake yet.

I set this in CMakeLists.txt:

set(CMAKE_VERBOSE_MAKEFILE ON)

so that I can get the command line when it was compiling:

C:\Software\LLVM\bin\clang++.exe --target=i686-pc-windows-msvc -O0 -g -Xclang -gcodeview -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -std=gnu++17 -MD -MT CMakeFiles\helloworld.dir\helloworld.cpp.obj -MF CMakeFiles\helloworld.dir\helloworld.cpp.obj.d -o CMakeFiles\helloworld.dir\helloworld.cpp.obj -c D:\git_test\helloworld.cpp

C:\Software\LLVM\bin\clang++.exe --target=i686-pc-windows-msvc -fuse-ld=lld-link -nostartfiles -nostdlib -O0 -g -Xclang -gcodeview -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -Xlinker /subsystem:console @CMakeFiles\helloworld.dir\objects1.rsp -o helloworld.exe -Xlinker /MANIFEST:EMBED -Xlinker /implib:helloworld.lib -Xlinker /pdb:D:\git_test\build\helloworld.pdb -Xlinker /version:0.0 @CMakeFiles\helloworld.dir\linkLibs.rsp

and then I copy these two commands to enter them by myself. I found if I remove these options:

-Xclang --dependent-lib=msvcrtd
-nostartfiles -nostdlib

the static program would be built.

I want know why and how can I edit my CMakeLists.txt to build a static program?

0

There are 0 best solutions below