1、生成程序可执行程序、静态库、动态库
a)源代码路径
set(SRC_CPP_FILES
${PROJECT_SOURCE_DIR}/src/dll.cpp export.def
${PROJECT_SOURCE_DIR}/src/pch.cpp
)
b)生成可执行程序
add_executable(hello ${SRC_CPP_FILES})
c)生成静态库
add_library(MyStaticLib STATIC ${SRC_CPP_FILES})
d)生成动态库
add_library(MyDll SHARED ${SRC_CPP_FILES})
2、设置程序的输出目录
设置相对于源代码路径PROJECT_SOURCE_DIR(CMakeLists.txt文件所在目录
a)设置可执行文件的输出路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../bin/win32)
MESSAGE (STATUS "exe PROJECT_BINARY_DIR :" ${PROJECT_SOURCE_DIR})
MESSAGE (STATUS "EXECUTABLE_OUTPUT_PATH :" ${EXECUTABLE_OUTPUT_PATH})
b)设置动态库输出路径(注意这个放在最后)
windows用这个:
#以下是设置动态库输出路径(注意这个放在最后)
SET_TARGET_PROPERTIES(QaxCollect PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../../bin/win32)
MESSAGE (STATUS "PROJECT_SOURCE_DIR :" ${PROJECT_SOURCE_DIR})
MESSAGE (STATUS "RUNTIME_OUTPUT_DIRECTORY :" ${RUNTIME_OUTPUT_DIRECTORY})
#以上是设置动态库输出路径
linux用以下
#以下是设置动态库输出路径(注意这个放在最后)
set_target_properties(QaxCollect PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../../bin/win32)
MESSAGE (STATUS "PROJECT_SOURCE_DIR :" ${PROJECT_SOURCE_DIR})
MESSAGE (STATUS "LIBRARY_OUTPUT_DIRECTORY :" ${LIBRARY_OUTPUT_DIRECTORY})
c)设置静态库输出路径
#以下是设置静态库输出路径(注意这个放在最后)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../../libs/)
MESSAGE (STATUS "LIBRARY_OUTPUT_PATH :" ${LIBRARY_OUTPUT_PATH})
#以上是设置静态库输出路径
3、设置库目录和依赖的静态库或者导入库
a)设置库目录
link_directories(${PROJECT_SOURCE_DIR}/../lib/win32)
b)设置依赖的静态库或者导入库名字(staticlib库是我测试用的库)
set(link_files kernel32
user32
gdi32
shlwapi
Thirdlib
)
c)link
target_link_libraries(demo_use_QaxCollect ${link_files})
4、定义编译开关
add_definitions(-DUNICODE -D_UNICODE)
add_definitions(-DGTEST_NOT_REAL_FILES)
add_definitions(-DUSER_WIN_H)
5、设置include包含目录
a)相对路径方法:
set(ThirdLib ${PROJECT_SOURCE_DIR}/../../../../pub)
include_directories("${ThirdLib}/include")
b)绝对路径方法:
set(publib "/usr/lib/boost_1_57_0/")
include_directories("${publib}/include")
MESSAGE (STATUS "publib/include :" ${publib}/include)
6、windows设置c和cpp运行时库MT链接方式
a)方法1:
#设置C++运行时库为MT
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
#设置C运行时库为MT
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MT")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
b)方法2:
if (MSVC)
set(CompilerFlags
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE
CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
endif(MSVC)
7、windows Release版本生成可以调试的pdb文件
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
8、linux编译链接库
-lz -lrt -lm -lc都是什么库
-lz 压缩库(Z)
-lrt 实时库(real time):shm_open系列
-lm 数学库(math)
-lc 标准C库(C lib)
-dl ,是显式加载动态库的动态函数库
9、linux动态库生成平台无关的代码
set(CMAKE_CXX_FLAGS "-fpic ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "-lrt ${CMAKE_CXX_FLAGS}")
10、引用windows上设置的环境变量
include_directories("$ENV{3RDPARTY_LIBRARY_ROOT}/")
11、递归查找指定目录下的文件(并生成so)
file(GLOB_RECURSE deffile ${PROJECT_SOURCE_DIR}/*.def)
file(GLOB_RECURSE SRC_CPP_FILES ${PROJECT_SOURCE_DIR}/*.cpp)
file(GLOB_RECURSE SRC_C_FILES ${PROJECT_SOURCE_DIR}/*.c)
add_library(v3update SHARED ${SRC_CPP_FILES}
${SRC_C_FILES}
${deffile})
12、linux平台上手工编写CMakelist并进行编译项目
a)编写CMakeLists.txt文件,拷贝以下内容保存到文件CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
project (testUpload)
#添加下面这句就是编译DEBUG版本,注释掉就是Release版本
add_definitions("-Wall -g")
include_directories("${PROJECT_SOURCE_DIR}/include")
link_directories(${PROJECT_SOURCE_DIR}/)
set(link_files dl
pthread
skydas_sdk
)
add_compile_options(-std=c++11)
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O0")
set(CMAKE_EXECUTABLE_SUFFIX ".out")
file(GLOB SRC_CPP_FILES ${PROJECT_SOURCE_DIR}/*.cpp)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build)
add_executable(testUpload ${SRC_CPP_FILES})
target_link_libraries(testUpload ${link_files})
b)编译文件,命令 cmake .
c)清空 命令 rm -rf CMakeFiles/
13、打印各种设置
message(STATUS "CMAKE_C_FLAGS = " ${CMAKE_C_FLAGS})
message(STATUS "CMAKE_C_FLAGS_DEBUG = " ${CMAKE_C_FLAGS_DEBUG})
message(STATUS "CMAKE_C_FLAGS_MINSIZEREL = " ${CMAKE_C_FLAGS_MINSIZEREL})
message(STATUS "CMAKE_C_FLAGS_RELEASE = " ${CMAKE_C_FLAGS_RELEASE})
message(STATUS "CMAKE_C_FLAGS_RELWITHDEBINFO = " ${CMAKE_C_FLAGS_RELWITHDEBINFO})
message(STATUS "CMAKE_CXX_FLAGS = " ${CMAKE_CXX_FLAGS})
message(STATUS "CMAKE_CXX_FLAGS_DEBUG = " ${CMAKE_CXX_FLAGS_DEBUG})
message(STATUS "CMAKE_CXX_FLAGS_MINSIZEREL = " ${CMAKE_CXX_FLAGS_MINSIZEREL})
message(STATUS "CMAKE_CXX_FLAGS_RELEASE = " ${CMAKE_CXX_FLAGS_RELEASE})
message(STATUS "CMAKE_CXX_FLAGS_RELWITHDEBINFO = " ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
message(STATUS "CMAKE_EXE_LINKER_FLAGS = " ${CMAKE_EXE_LINKER_FLAGS})
message(STATUS "CMAKE_EXE_LINKER_FLAGS_DEBUG = " ${CMAKE_EXE_LINKER_FLAGS_DEBUG})
message(STATUS "CMAKE_EXE_LINKER_FLAGS_MINSIZEREL = " ${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL})
message(STATUS "CMAKE_EXE_LINKER_FLAGS_RELEASE = " ${CMAKE_EXE_LINKER_FLAGS_RELEASE})
message(STATUS "CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO = " ${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO})
message(STATUS "CMAKE_MODULE_LINKER_FLAGS = " ${CMAKE_MODULE_LINKER_FLAGS})
message(STATUS "CMAKE_MODULE_LINKER_FLAGS_DEBUG = " ${CMAKE_MODULE_LINKER_FLAGS_DEBUG})
message(STATUS "CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL = " ${CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL})
message(STATUS "CMAKE_MODULE_LINKER_FLAGS_RELEASE = " ${CMAKE_MODULE_LINKER_FLAGS_RELEASE})
message(STATUS "CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO = " ${CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO})
message(STATUS "CMAKE_SHARED_LINKER_FLAGS = " ${CMAKE_SHARED_LINKER_FLAGS})
message(STATUS "CMAKE_SHARED_LINKER_FLAGS_DEBUG = " ${CMAKE_SHARED_LINKER_FLAGS_DEBUG})
message(STATUS "CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL = " ${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL})
message(STATUS "CMAKE_SHARED_LINKER_FLAGS_RELEASE = " ${CMAKE_SHARED_LINKER_FLAGS_RELEASE})
message(STATUS "CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO = " ${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO})
message(STATUS "CMAKE_STATIC_LINKER_FLAGS = " ${CMAKE_STATIC_LINKER_FLAGS})
message(STATUS "CMAKE_STATIC_LINKER_FLAGS_DEBUG = " ${CMAKE_STATIC_LINKER_FLAGS_DEBUG})
message(STATUS "CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL = " ${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL})
message(STATUS "CMAKE_STATIC_LINKER_FLAGS_RELEASE = " ${CMAKE_STATIC_LINKER_FLAGS_RELEASE})
message(STATUS "CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO = " ${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO})
14、一个demo
cmake_minimum_required (VERSION 3.8)
project (src)
if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux")
set(ThirdLib "/usr/lib/boost_1_57_0/")
include_directories("${ThirdLib}/include")
MESSAGE (STATUS "ThirdLib/include :" ${ThirdLib}/include)
include_directories("./src")
set(link_files dl
pthread
)
add_compile_options(-std=c++11)
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O0 -g")
set(CMAKE_CXX_FLAGS "-fpic ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "-lrt ${CMAKE_CXX_FLAGS}")
set(SRC_CPP_FILES
${PROJECT_SOURCE_DIR}/src/main2.cpp
)
add_executable(Hello ${SRC_CPP_FILES}
${VersionFilesOutputVariable})
target_link_libraries(Hello ${link_files})
#以下是设置可执行文件的输出路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../bin/linux_debug)
MESSAGE (STATUS "exe PROJECT_BINARY_DIR :" ${PROJECT_SOURCE_DIR})
MESSAGE (STATUS "EXECUTABLE_OUTPUT_PATH :" ${EXECUTABLE_OUTPUT_PATH})
#以上是设置可执行文件的输出路径
elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Windows")
set(ThirdLib ${PROJECT_SOURCE_DIR}/../../../../ThirdLib)
add_definitions(-DUNICODE -D_UNICODE)
add_definitions(-DGTEST_NOT_REAL_FILES)
add_definitions(-DUSER_WIN_H)
add_definitions(-D_CONSOLE)
include_directories("${ThirdLib}/include")
include_directories("./src")
MESSAGE (STATUS "ThirdLib/include :" ${ThirdLib}/include)
set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "/NODEFAULTLIB:MSVCRT.lib ${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "/NODEFAULTLIB:MSVCRT.lib ${CMAKE_SHARED_LINKER_FLAGS_RELEASE}")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NODEFAULTLIB:MSVCRTD.lib ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
##[[
#设置C++运行时库为MT
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
#设置C运行时库为MT
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MT")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
#]]
#通过下面这几句设置为MT链接方式也可以
#[[
if (MSVC)
set(CompilerFlags
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE
CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
endif(MSVC)
]]
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
set(link_files kernel32
user32
gdi32
)
SET (CMAKE_MODULE_PATH "${ThirdLib}/build/cmake_product_version;${CMAKE_MODULE_PATH}")
MESSAGE (STATUS "CMAKE_MODULE_PATH :" ${CMAKE_MODULE_PATH})
include(generate_product_version)
generate_product_version(
VersionFilesOutputVariable
NAME "Hello.exe"
VERSION_MAJOR 1
VERSION_MINOR 0
VERSION_PATCH ${BUILD_COUNTER}
VERSION_REVISION 1001
)
set(SRC_CPP_FILES
${PROJECT_SOURCE_DIR}/src/getopt.c
${PROJECT_SOURCE_DIR}/src/main.cpp
)
add_executable(Hello ${SRC_CPP_FILES}
${VersionFilesOutputVariable})
target_link_libraries(Hello ${link_files})
#以下是设置可执行文件的输出路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../bin/win32)
MESSAGE (STATUS "exe PROJECT_BINARY_DIR :" ${PROJECT_SOURCE_DIR})
MESSAGE (STATUS "EXECUTABLE_OUTPUT_PATH :" ${EXECUTABLE_OUTPUT_PATH})
#以上是设置可执行文件的输出路径
endif()
15、另外一个demo
# CMakeList.txt: CMakeProject 的 CMake 项目,在此处包括源代码并定义
# 项目特定的逻辑。
#
cmake_minimum_required (VERSION 3.8)
PROJECT(Hello)
# set env
if(CMAKE_COMPILER_IS_GNUCXX)
add_compile_options(-std=c++11)
message(STATUS "optional:-std=c++11")
message(STATUS "use c17inject")
else()
add_compile_options(-std=c++17)
message(STATUS "optional:-std=c++17")
endif(CMAKE_COMPILER_IS_GNUCXX)
# common env (win&linux)
MESSAGE(STATUS "COMMON_SRC_ROOT=$ENV{COMMON_SRC_ROOT}")
MESSAGE(STATUS "LINUX_LIBRARY_PATH=$ENV{LINUX_LIBRARY_PATH}")
MESSAGE(STATUS "3RDPARTY_LIBRARY_ROOT=$ENV{3RDPARTY_LIBRARY_ROOT}")
MESSAGE(STATUS "PUBLIC_LIBRARY_PATH=$ENV{PUBLIC_LIBRARY_PATH}")
IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
##################################Linux System Start#######################################
MESSAGE(STATUS "current platform: Linux ")
# 预编译宏
add_definitions(-D__LINUX__)
add_definitions(-DUNICODE -D_UNICODE)
set(ENV{LINUX_LIBRARY_PATH} /usr/public/linux)
set(ENV{3RDPARTY_LIBRARY_ROOT} /usr/public/3rdparty)
set(ENV{COMMON_SRC_ROOT} /usr/public/common)
# include headers
include_directories("$ENV{LINUX_LIBRARY_PATH}/Include")
include_directories("$ENV{3RDPARTY_LIBRARY_ROOT}/")
include_directories("$ENV{COMMON_SRC_ROOT}")
set(HELLO_SRC
${PROJECT_SOURCE_DIR}/src/Hello.cpp
${PROJECT_SOURCE_DIR}/src/pch.cpp
)
# gene so
add_library(HelloTest SHARED
${HELLO_SRC}
)
set(CMAKE_CXX_FLAGS "-fpic ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "-lrt ${CMAKE_CXX_FLAGS}")
# ext op
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb") # Debug模式下的编译指令
#SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall") # Release模式下的编译指令
# add link lib
TARGET_LINK_LIBRARIES(HelloTest dl)
ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux")
##################################Linux System End#######################################
##################################Windows System Start#####################################
IF (CMAKE_SYSTEM_NAME MATCHES "Windows")
MESSAGE(STATUS "current platform: Windows")
add_definitions(-DUNICODE -D_UNICODE)
add_compile_options(-std=c++11)
# include headers GLOBAL_PUBLIC_LIBRARY_PATH和3RDPARTY_LIBRARY_ROOT和COMMON_SRC_ROOT是windows上设置的环境变量
include_directories("$ENV{PUBLIC_LIBRARY_PATH}/Include")
include_directories("$ENV{3RDPARTY_LIBRARY_ROOT}/")
include_directories("$ENV{COMMON_SRC_ROOT}")
# gene dll
set(HELLO_SRC
${PROJECT_SOURCE_DIR}/src/dll.cpp HelloTest.def
${PROJECT_SOURCE_DIR}/src/hello.cpp
)
# set product version cmake modules path:
SET (CMAKE_MODULE_PATH "$ENV{COMMON_SRC_ROOT}/cmake_product_version/;${CMAKE_MODULE_PATH}")
MESSAGE (STATUS "CMAKE_MODULE_PATH :" ${CMAKE_MODULE_PATH})
include_directories("${CMAKE_MODULE_PATH}/")
include(generate_product_version)
generate_product_version(
VersionFilesOutputVariable
NAME "HelloTest.dll"
VERSION_MAJOR 10
VERSION_MINOR 0
VERSION_PATCH ${BUILD_COUNTER}
VERSION_REVISION 1030
)
add_library(HelloTest SHARED
${HELLO_SRC}
${VersionFilesOutputVariable}
)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
SET(BASE_LIB "$ENV{PUBLIC_LIBRARY_PATH}/Lib/Windows")
if(CMAKE_CL_64)
target_link_libraries(HelloTest "${BASE_LIB}/mt_64.lib")
else()
target_link_libraries(HelloTest "${BASE_LIB}/mt_32.lib")
endif()
#SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) #设置可执行文件的输出目录
ENDIF (CMAKE_SYSTEM_NAME MATCHES "Windows")
##################################Windows System End#####################################
16、vs cmake远程调试
右击项目->添加调试配置->修改launch.vs.json
window配置(修改带注释的5项),然后启动远程调试msvsmon.exe
{
"type": "remoteWindows", //远程调试
"authenticationType": "none", //不验证密码账号
"project": "CMakeLists.txt",
"projectTarget": "hello.exe (F:\\prac\\hello.exe)",
"name": "hello.exe (F:\\test2\\hello.exe)",
"remoteMachineName": "192.168.1.168", //远程ip
"cwd": "C:\\Users\\Administrator\\Desktop\\Test", //要调试的远程的文件夹路径
"program": "C:\\Users\\Administrator\\Desktop\\Test\\hello.exe" //要远程调试的的可执行程序路径
}
linux配置
CMakeLists.txt中,设置可执行程序的输出路径:SET(EXECUTABLE_OUTPUT_PATH /home/root/prac) ,然后修改launch.vs.json,修改program字段就行了
{
"type": "cppgdb",
"name": "hello",
"project": "CMakeLists.txt",
"projectTarget": "hello",
"comment": "了解如何配置远程调试。有关详细信息,请参阅 http://aka.ms/vslinuxdebug",
"debuggerConfiguration": "gdb",
"MIMode": "gdb",
"args": [],
"env": {},
"program": "/home/root/prac/hello" //这里选择要调试的位置
}
17、设置生成的elf文件名称
#设置生成的elf文件名称
set(OUT_NAME "test.so")
set(parent_path ../../)
set(srcDir ${parent_path}src)
set(src_files
${srcDir}/main.cpp
)
add_executable(${OUT_NAME} ${src_files})
#设置生成的elf文件名前缀!!!!!!!!
set_target_properties(${OUT_NAME} PROPERTIES PREFIX "")
#设置生成的elf文件名后缀!!!!!!!!
set_target_properties(${OUT_NAME} PROPERTIES SUFFIX "")
#设置link库
target_link_libraries(${OUT_NAME}
dl
pthread
rt
)
18、设置编译Debug或者Release版本
在CMakeList.txt最后面增加以下代码,编译的时候指定cmake .. -DCMAKE_BUILD_TYPE=Debug
if (CMAKE_BUILD_TYPE MATCHES "Debug")
message(" cmake build type : Debug")
else()
message(" cmake build type : not Debug")
add_custom_command(
TARGET ${OUT_NAME}
POST_BUILD
COMMAND strip ${OUT_NAME}
)
endif(CMAKE_BUILD_TYPE MATCHES "Debug")
19、other demo
# CMakeList.txt: CMake 项目
cmake_minimum_required (VERSION 3.4)
project ("Hello")
set(OUT_NAME "Hello.so")
set(parent_path ../../../../)
set(3rd_open_src ${parent_path}poco)
#判断编译器类型,如果是g++编译器,则在编译选项中加入c++11支持
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-pthread -std=c++98 ${CMAKE_CXX_FLAGS}")
message(STATUS "sangtest CMAKE_COMPILER_IS_GNUCXX")
else()
# add_compile_options(-std=c++11)
set(CMAKE_CXX_FLAGS "-pthread -std=c++11 ${CMAKE_CXX_FLAGS}")
endif(CMAKE_COMPILER_IS_GNUCXX)
set(include_path "../../xml/include"
"${3rd_open_src}/include")
message(STATUS "sangtest 3rd_open_src = " ${3rd_open_src})
message(STATUS "sangtest include_path = " ${include_path})
set(lib_path "../../xml/lib"
"/usr/lib"
"${3rd_open_src}/lib")
include_directories(../../include)
include_directories(${include_path})
link_directories(${lib_path})
file(TO_CMAKE_PATH ${3rd_open_src}/include/test/*.cpp OTHER_FILES)
file(GLOB_RECURSE OHTER_CPP_FILES ${OTHER_FILES})
set(src_files
${PROJECT_SOURCE_DIR}/tool.cpp
${PROJECT_SOURCE_DIR}/main.cpp
)
#Generate the shared library from the library sources
#add_library(${OUT_NAME} SHARED ${src_files} ${jsonfile})
add_executable(${OUT_NAME} ${src_files}
${OHTER_CPP_FILES})
set_target_properties(${OUT_NAME} PROPERTIES PREFIX "")
set_target_properties(${OUT_NAME} PROPERTIES SUFFIX "")
target_link_libraries(${OUT_NAME}
dl
pthread
rt
poco.a
)
if (CMAKE_BUILD_TYPE MATCHES "Debug")
message(" cmake build type : Debug")
else()
message(" cmake build type : not Debug")
add_custom_command(
TARGET ${OUT_NAME}
POST_BUILD
COMMAND strip ${OUT_NAME}
)
endif(CMAKE_BUILD_TYPE MATCHES "Debug")