I am trying to build a c 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)
...
cuimb\CMakeLists.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 "")
Related
I'm new in CMake.
Could someone please describe to me what I should write in my CMakeLists.txt to copy dependencies to the output directory of the executable file?
So, I have a CMake project:
# top-level CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
project(CoolLib VERSION 0.0.1)
set(CMAKE_CXX_STANDARD 23)
add_subdirectory(mylib)
add_subdirectory(mytestconsole)
install(TARGETS mnconsole mnlib
RUNTIME DESTINATION bin COMPONENT Runtime
LIBRARY DESTINATION lib COMPONENT Runtime
ARCHIVE DESTINATION lib/myproject COMPONENT Development)
# mylib/CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
project(mylib VERSION 0.0.1)
add_library(mylib SHARED mylib.cpp mylib.h)
target_include_directories(mylib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
# mytestconsole/CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
project(mytestconsole VERSION 0.0.1)
add_executable(mytestconsole main.cpp)
target_link_libraries(mytestconsole LINK_PUBLIC mylib)
target_include_directories(mytestconsole PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/mylib)
As you can see, I've added an install() command, but this isn't exactly what I want.
As far as I understand, the install() command will copy my program to the Program Files directory in Windows.
GOAL:
I want to copy the mingw libraries (e.g. libgcc_s_seh-1, libstdc++-6, libwinpthread-1, etc.) and mylib to the output directory of the mytestconsole.exe file.
PS: I saw this answer, but GetPrerequesites is deprecated. And I didn't understand how to use it.
I tried to do this:
add_custom_command(
TARGET mnconsole POST_BUILD
COMMAND LIST_PREREQUISITES($<TARGET_FILE:mnconsole>)
VERBATIM)
But got an error:
D:\Programs\JetBrains\CLion\bin\cmake\win\bin\cmake.exe --build D:\Projects\MethaneNumber\_builds\debug --target mnconsole -j 9
[1/1] Linking CXX executable mnconsole\mnconsole.exe
FAILED: mnconsole/mnconsole.exe
cmd.exe /C "cd . && D:\Programs\mingw64\12.2.0\x64\posix-seh\bin\c++.exe -g mnconsole/CMakeFiles/mnconsole.dir/main.cpp.obj -o mnconsole\mnconsole.exe -Wl,--out-implib,mnconsole\libmnconsole.dll.a -Wl,--major-image-version,0,--minor-image-version,0 mnlib/libmnlib.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cmd.exe /C "cd /D D:\Projects\MethaneNumber\_builds\debug\mnconsole && LIST_PREREQUISITES ( D:/Projects/MethaneNumber/_builds/debug/mnconsole/mnconsole.exe )""
"LIST_PREREQUISITES" is not recognized as an internal command.
ninja: build stopped: subcommand failed.
PS2: It should work like windeployqt.exe with Qt, but I'm not using Qt in this project right now.
project(Direct_Learning)
set(target Sample ${CMAKE_CURRENT_SOURCE_DIR}/Src/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Src/RaytracingHlslCompat.h)
set(HLSL_SHADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/Src/Raytracing.hlsl)
set_property(SOURCE ${SHADER_FILES} PROPERTY VS_SHADER_MODEL 4.0_level_9_3)
set_property(SOURCE ${SHADER_FILES} PROPERTY VS_SHADER_FLAGS "/DFLAGS_ADDED /Fh \"$(OutDir)%(Filename).h\"")
add_executable(${target} ${HLSL_SHADER_FILES})
I tried to compile a header with a shader bytecode, but I got this error
Error X3000: unrecognized identifier 'RaytracingAccelerationStructure'
Also tried to compile into an object file
project(Direct_Learning)
set(target Sample ${CMAKE_CURRENT_SOURCE_DIR}/Src/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Src/RaytracingHlslCompat.h)
set(HLSL_SHADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/Src/Raytracing.hlsl)
# Build HLSL shaders
add_custom_target(shaders)
set_source_files_properties(${HLSL_SHADER_FILES} PROPERTIES ShaderModel "4_0_level_9_3")
foreach(FILE ${HLSL_SHADER_FILES})
get_filename_component(FILE_WE ${FILE} NAME_WE)
get_source_file_property(shadertype ${FILE} ShaderType)
get_source_file_property(shadermodel ${FILE} ShaderModel)
add_custom_command(TARGET shaders
COMMAND fxc.exe /nologo /Emain /T${shadertype}_${shadermodel} $<$<CONFIG:DEBUG>:/Od> /Zi /Fo ${CMAKE_BINARY_DIR}/${FILE_WE}.cso /Fd ${CMAKE_BINARY_DIR}/${FILE_WE}.pdb ${FILE}
MAIN_DEPENDENCY ${FILE}
COMMENT "HLSL ${FILE}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
VERBATIM)
endforeach(FILE)
add_executable(${target} ${shaders})
But no "cso" file is created
What am I doing wrong?
I have generated code using cubeMX and while compiling it gives me the following errors. I have already tried several fixes but none of them is working.
/tmp/ccvxTV8U.s: Assembler messages:
/tmp/ccvxTV8U.s:758: Error: selected processor does not support `vstmdbeq r0!,{s16-s31}' in Thumb mode
/tmp/ccvxTV8U.s:760: Error: instruction not allowed in IT block -- `stmdb r0!,{r4-r11,r14}'
/tmp/ccvxTV8U.s:782: Error: selected processor does not support `vldmiaeq r0!,{s16-s31}' in Thumb mode
/tmp/ccvxTV8U.s:784: Error: instruction not allowed in IT block -- `msr psp,r0'
CMakeLists.txt
IDE: CLion 2019.3
#THIS FILE IS AUTO GENERATED FROM THE TEMPLATE! DO NOT CHANGE!
SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.13)
# specify cross compilers and tools
SET(CMAKE_C_COMPILER_WORKS 1)
SET(CMAKE_C_COMPILER arm-none-eabi-gcc)
SET(CMAKE_CXX_COMPILER_WORKS 1)
SET(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
set(SIZE arm-none-eabi-size)
SET(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F767ZITx_FLASH.ld)
#Uncomment for hardware floating point
SET(FPU_FLAGS "-mfloat-abi=hard -mfpu=fpv4-sp-d16")
add_definitions(-DARM_MATH_CM4 -DARM_MATH_MATRIX_CHECK -DARM_MATH_ROUNDING -D__FPU_PRESENT=1)
#Uncomment for software floating point
#SET(FPU_FLAGS "-mfloat-abi=soft")
SET(COMMON_FLAGS
"-mcpu=cortex-m7 ${FPU_FLAGS} -mthumb -mthumb-interwork -ffunction-sections -fdata-sections \
-g -fno-common -fmessage-length=0 -specs=nosys.specs -specs=nano.specs")
SET(CMAKE_CXX_FLAGS_INIT "${COMMON_FLAGS} -std=c++11")
SET(CMAKE_C_FLAGS_INIT "${COMMON_FLAGS} -std=gnu99")
SET(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,-gc-sections,--print-memory-usage -T ${LINKER_SCRIPT}")
PROJECT(freertos_test C CXX ASM)
set(CMAKE_CXX_STANDARD 11)
#add_definitions(-DARM_MATH_CM4 -DARM_MATH_MATRIX_CHECK -DARM_MATH_ROUNDING -D__FPU_PRESENT=1)
add_definitions(-D__weak=__attribute__\(\(weak\)\) -D__packed=__attribute__\(\(__packed__\)\) -DUSE_HAL_DRIVER -DSTM32F767xx)
file(GLOB_RECURSE SOURCES "Middlewares/*.*" "startup/*.*" "Drivers/*.*" "Core/*.*")
include_directories(Core/Inc Drivers/STM32F7xx_HAL_Driver/Inc Drivers/STM32F7xx_HAL_Driver/Inc/Legacy Drivers/CMSIS/Device/ST/STM32F7xx/Include Drivers/CMSIS/Include Middlewares/Third_Party/FreeRTOS/Source/include Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM7/r0p1)
add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})
set(CMAKE_EXE_LINKER_FLAGS
"${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map")
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
I'm using the NUCLEO-F767ZI with a STM32F767
file(GLOB_RECURSE SOURCES "Middlewares/*.*"
You are compiling FreeRTOS for all possible implementations for all possible cpus, so you are also compiling sources not compatible at all with your current cpu. You should compile sources only for your destination cpu.
That would most probably be, assuming the folder Middlewares is taken from cubemx:
file(GLOB src1
Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F/*.c
Middlewares/Third_Party/FreeRTOS/Source/*.c
)
And add only the needed include path:
target_include_directories(YOUR_TARGET PUBLIC
Middlewares/Third_Party/FreeRTOS/Source/include
)
I'm doing C++ using libcURL. I got the task to test functions in Travis CI. So I used .travis.yml to get libcurl-dev and libssl-dev to connect to the project. I installed them and checked cURL configuration:
SSL support: enabled (OpenSSL)
But as I was trying to make main test file i got list of errors (example)
`Linking CXX executable ../bin/TechProject_example
/usr/local/lib/libcurl.a(libcurl_la-openssl.o): In function ossl_recv:
openssl.c:(.text+0xf3): undefined reference to ERR_clear_error
openssl.c:(.text+0x11c): undefined reference to SSL_read
openssl.c:(.text+0x163): undefined reference to SSL_get_error`
and so on...
The only thing I was able to check is that libcurl is working properly. I placed
--without-ssl flag and got protocol error.
Also I used
`- sudo ln -fs /usr/lib/libcurl.so.4 /usr/local/lib/`
to avoid cmake error. (I'm not very good at UNIX command, so this can be a problem)
So how should properly connect openSSL to C++ project that has <curl/curl.h> ?
EDIT:
Here is CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project (TechProject)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(HEADERS ${PROJECT_SOURCE_DIR}/include/testclass.h)
set(HEADERS ${PROJECT_SOURCE_DIR}/include/catch.hpp)
set(SOURCES ${PROJECT_SOURCE_DIR}/sources/testclass.cpp)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
include_directories(${PROJECT_SOURCE_DIR}/include)
add_library (${PROJECT_NAME} STATIC ${SOURCES})
add_subdirectory(example)
add_subdirectory(tests)
find_library(FOO_LIB libcurl.a)
target_link_libraries(TechProject "${FOO_LIB}")
And CMakeLists.txt in tests subdir:
cmake_minimum_required(VERSION 2.8)
set(TESTS_FOR_PROJECT TechProject)
project(${TESTS_FOR_PROJECT}_tests)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
option(BUILD_SHARED_LIBS "Build shared instead of static library" OFF)
file(GLOB ${PROJECT_NAME}_sources "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
find_package(${TESTS_FOR_PROJECT})
include_directories(${${TESTS_FOR_PROJECT}_INCLUDE_DIRS}})
include_directories(${CPM_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_sources} ${${PROJECT_NAME}_headers})
target_link_libraries(${PROJECT_NAME} ${TESTS_FOR_PROJECT})
add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_CURRENT_SOURCE_DIR}/fixtures $<TARGET_FILE_DIR:${PROJECT_NAME}>)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${PROJECT_NAME} -s -r compact WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
I am a newbie to cmake. I want to add a new flag to be applied for my module build which uses cmake as build tool. Am trying to add the flag in CMakeLists.txt but the changes are not reflected. Should I apply the changes to some other file? I tried cleaning using
$ cmake clean .
but still the problem exists.
Request help.
Regards
Santosh
If you mean compiler flags then you can do something like below, note I am a newbie too so there might well be better ways:
if( MSVC )
set( CMAKE_CXX_FLAGS " /DWIN32 /W3 /GX /GR /Wp64 /Zc:forScope" )
set( CMAKE_CXX_FLAGS_DEBUG " /D_DEBUG /MDd /Zi /Ob0 /Od /GZ /Gm /RTC1 /ZI" )
elseif( CMAKE_COMPILER_IS_GNUCXX )
set( CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} " -ansi -Winvalid-pch" )
endif()