【问题标题】:CMakeLists C program with gtest(C++)CMakeLists C 程序与 gtest(C++)
【发布时间】:2015-08-06 22:31:28
【问题描述】:

我想创建一个输出两个版本的可执行文件的 CMakeLists。一个将是我的 C 应用程序的发布版本。 另一个是我的应用的 gtest 版本。

cmake_minimum_required(VERSION 3.1)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "C:\\Users\\James\\ClionProjects\\DustAgent\\build")

project(DustAgent)

include_directories ( WindowsApi gtest-1.7.0/include )

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -std=c99 -pthread")

set(SOURCE_FILES main.c utilities/utilities.c utf8/utf8.c)
set(GTEST_SOURCE_FILES ${SOURCE_FILES} gtest-1.7.0/src/gtest-all.cc)
add_executable(DustAgent ${SOURCE_FILES})

如何使第一个 exe 不需要 google 库以及如何将 c++ 的特定 gcc 选项提供给 gtest 版本?

【问题讨论】:

    标签: c++ c cmake


    【解决方案1】:

    您应该首先创建一个库,然后将可执行文件和测试都链接到它。不过,可执行文件和测试应该有单独的源代码。

    add_library(DustAgentLibrary utilities/utilities.c utf8/utf8.c)
    
    add_executable(DustAgent main.c)
    target_link_libraries(DustAgent DustAgentLibrary)
    
    add_executable(DustAgentTest test.c)
    target_link_libraries(DustAgentTest DustAgentLibrary gtest)
    add_test(DustAgentTest DustAgentTest)
    

    【讨论】:

    • 如何为每个库和可执行文件指定不同的编译器指令?另外,我如何确保我的主要可执行文件不使用 STL 编译?
    • 将 COMPILE_FLAGS 与 set_target_properties 一起使用:stackoverflow.com/questions/5096881/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    相关资源
    最近更新 更多