CMake重写本地编译器标志

我正在尝试使用Visual Studio 14构建使用CMake的ac项目,但是我无法覆盖由CMake设置的导致项目无法构建的编译器标志。 我在我的CMakeLists.txt文件中使用这种方法

set(flags /nologo /c /EHsc /GS- /MTd /Od /TC /Zi /Zp2 /D _USING_V110_SDK71_)
set(CMAKE_C_FLAGS ${flags})
set(rc_flags /l0x809)
set(CMAKE_RC_FLAGS ${rc_flags})
set(linker_flags /NOLOGO /SUBSYSTEM:console /MACHINE:I386 /DLL)
set(CMAKE_LINKER_FLAGS ${linker_flags})

任何帮助将不胜感激!

编辑:主CMakeLists.txt的完整示例在这里

cmake_minimum_required(VERSION 3.4)
project(cuism C)

set(WINDOWS_SDK_INCLUDES "C:/Program Files (x86)/Microsoft  SDKs/Windows/v7.1A/Include/")
set(WINDOWS_SDK_LIBRARIES "C:/Program Files (x86)/Microsoft SDKs/Windows/v7.1A/Lib")
set(WSOCK "WSock32.lib")
set(ODBC "odbc32.lib")
set(ODBCCP "odbccp32.lib")

set(includeDir "${CMAKE_SOURCE_DIR}/include/")
set(resourceDir "${CMAKE_SOURCE_DIR}/resources/")


include_directories(
    ${includeDir}
    ${resourceDir}
)

set(CMAKE_LIBRARY_PATH ${WINDOWS_SDK_LIBRARIES})
set(CMAKE_INCLUDE_PATH ${WINDOWS_SDK_INCLUDES})


set(flags "/nologo /c /EHsc /GS- /MTd /Od /TC /Zi /Zp2 /D _USING_V110_SDK71_" )
set(CMAKE_C_FLAGS ${flags})

set(rc_flags /l0x809)
set(CMAKE_RC_FLAGS ${rc_flags})

set(linker_flags /NOLOGO /SUBSYSTEM:console /DLL)
set(CMAKE_LINKER_FLAGS ${linker_flags})

message(c flags are ${CMAKE_C_FLAGS})
message(rc flags are ${CMAKE_RC_FLAGS})
message(linker flags are ${CMAKE_LINKER_FLAGS})
message(linker EXE flags are ${CMAKE_EXE_LINKER_FLAGS})

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)


ADD_SUBDIRECTORY(cuimbct)
ADD_SUBDIRECTORY(cuimb)
ADD_SUBDIRECTORY(cuidll)

每个sudbdirectory都有一个类似于这个的CMakeLists.txt

set(src cuimb.c "${includeDir}port.c" cuimben.c cuimb.rc)
add_library(cuimb SHARED ${src})
target_link_libraries(cuimb ${WSOCK} ${ODBC} ${ODBCCP})

由于错误代码LNK2019,错误处于链接阶段


我已经给你的代码尝试了一下,并进行了一些小的修改,可以成功地应用这些编译器/链接器选项。 它包括我和@Tsyvarev所提出的更改(引用和使用CMAKE_SHARED_LINKER_FLAGS ),并且已经删除了对Windows SDK 7.1的静态引用。 这可以使用-T工具集选项进行设置:

> cmake -H"." -B"buildWin7" -T"v140_xp" -G"Visual Studio 14 2015" 
...

的CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(cuism C)

set(includeDir "${CMAKE_SOURCE_DIR}/include/")
set(resourceDir "${CMAKE_SOURCE_DIR}/resources/")

include_directories(
    ${includeDir}
    ${resourceDir}
)

set(flags "/nologo /c /EHsc /GS- /MTd /Od /TC /Zi /Zp2" )
set(CMAKE_C_FLAGS "${flags}")

set(rc_flags "/l0x809")
set(CMAKE_RC_FLAGS "${rc_flags}")

set(linker_flags "/NOLOGO /SUBSYSTEM:console /DLL")
set(CMAKE_SHARED_LINKER_FLAGS "${linker_flags}")

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

...

cuimb 的CMakeLists.txt

...

add_library(cuimb SHARED ${src})
target_link_libraries(cuimb wsock32 odbc32 odbccp32)

建议

为了保持您的CMake脚本代码清洁,您应该将所有编译器/链接器选项移至工具链文件。 我添加了一些我通常在那里定义的选项。 另外,我将它减少为单个配置MyDebug (只是Debug将附加其他调试选项),因为您的选项似乎不是用于Release

> cmake -H"." -B "buildWin7" -DCMAKE_TOOLCHAIN_FILE:PATH="VS2015Toolchain.cmake" -G"Visual Studio 14 2015" 
...

VS2015Toolchain.cmake

if (NOT CMAKE_GENERATOR_TOOLSET)
    set(CMAKE_GENERATOR_TOOLSET "v140_xp" CACHE INTERNAL "")
endif()

set(CMAKE_CONFIGURATION_TYPES "MyDebug" CACHE INTERNAL "")

# NOTE: Standard is a console app. If you need a windows app, 
#       use WIN32 define in add_executable
set(CMAKE_WIN32_EXECUTABLE 0 CACHE INTERNAL "")

set(CMAKE_C_FLAGS "/nologo /c /EHsc /GS- /MTd /Od /TC /Zi /Zp2 /W4" CACHE INTERNAL "")
set(CMAKE_RC_FLAGS "/l0x809" CACHE INTERNAL "")
set(
    CMAKE_SHARED_LINKER_FLAGS 
    "/NOLOGO /SAFESEH:NO /INCREMENTAL:NO /debug" 
    CACHE INTERNAL ""
)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH CACHE INTERNAL "")
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY CACHE INTERNAL "")
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY CACHE INTERNAL "")
链接地址: http://www.djcxy.com/p/24467.html

上一篇: CMake override native compiler flags

下一篇: Compiling nginx using CLion (via CMake)