CMake override native compiler flags

I am trying to build ac project with CMake using Visual Studio 14 however I cannot override the compiler flags being set by CMake which are causing project to fail to build. I am using this method in my CMakeLists.txt file

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})

Any help would greatly be appreciated!

Edit: full example of main CMakeLists.txt is here

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)

Each sudbdirectory has a CMakeLists.txt similar to this one

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

Error is during linking stage due to error code LNK2019


I've given your code a try and - with some small modifications - could successfully apply those compiler/linker options. It includes the changes I and @Tsyvarev have suggested (quotes and use of CMAKE_SHARED_LINKER_FLAGS ) and I've removed the static references to Windows SDK 7.1. This can be set with the -T toolset option:

> 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)

...

cuimbCMakeLists.txt

...

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

Recommendation

To keep your CMake script code clean, you should move all of the compiler/linker options to a toolchain file. And I've added some of the options I have normally defined there. Plus I reduced it to a single configuration MyDebug (just Debug would append other Debug options), because your options don't seem to be made for 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/24468.html

上一篇: 有没有可靠的方法来检测io.js或node.js是否在运行?

下一篇: CMake重写本地编译器标志