修修改改很久才有一定效果,但是还没有达到预定效果,先做一下记录
message("CMAKE_INSTALL_PREFIX:${CMAKE_INSTALL_PREFIX}")
aux_source_directory(. src_files)
add_library(test_header STATIC ${src_files})
install(TARGETS test_header DESTINATION ${CMAKE_SOURCE_DIR}/install)
install到指定文档
project(GtestSample2)
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
#add_library(${PROJECT_NAME} SHARED gtest)
add_library(${PROJECT_NAME} SHARED ${PROJECT_SOURCE_DIR}/src/function.cpp)
add_subdirectory("src")
add_subdirectory("test")
add_subdirectory("gtest")
enable_testing()
include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/gtest/googletest/include)
include_directories(${CMAKE_SOURCE_DIR}/gtest/googlemock/include)
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install)
install(TARGETS src gtest LIBRARY DESTINATION lib)
生成同名的lib和dll,引自cmake:同时生成根名相同的动态和静态库 - 知乎 (zhihu.com)
project(hello-world C)
add_subdirectory(hello)
add_subdirectory(world)
option(BUILD_STATIC_LIB "build libhello-world.a static lib" OFF) #默认不生成静态库
add_library(${PROJECT_NAME} SHARED hello_world.c)
target_link_libraries(${PROJECT_NAME} PRIVATE hello)
target_link_libraries(${PROJECT_NAME} PRIVATE world)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/hello)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/world)
if(BUILD_STATIC_LIB)
set(ORIGINAL_STATIC_LIB_NAME ${PROJECT_NAME}-static) #设置静态库的原始名称
add_library(${ORIGINAL_STATIC_LIB_NAME} STATIC hello_world.c)
set_target_properties(${ORIGINAL_STATIC_LIB_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
target_link_libraries(${ORIGINAL_STATIC_LIB_NAME} PRIVATE hello)
target_link_libraries(${ORIGINAL_STATIC_LIB_NAME} PRIVATE world)
target_include_directories(${ORIGINAL_STATIC_LIB_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/hello)
target_include_directories(${ORIGINAL_STATIC_LIB_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/world)
endif()
生成多个可执行文件
set(CMAKE_CXX_STANDARD 14)
#include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/gtest/googletest/include)
include_directories(${CMAKE_SOURCE_DIR}/gtest/googlemock/include)
include_directories(${CMAKE_SOURCE_DIR}/rtk_include_test)
link_libraries(src gtest_main gmock gmock_main test_header)
add_executable(test1 test1.cpp)
add_executable(test2 test2.cpp)
add_executable(test3 test3.cpp)
add_executable(test4 test4.cpp)
add_executable(test5 test5.cpp)
add_executable(test6 test6.cpp)
add_executable(test7 test7.cpp)