When I do this:
set_target_properties(${TARGET_BASENAME_A} PROPERTIES COMPILE_FLAGS "--std=c99" )
It sets the flag --std=c99 in both compiler and assembler command lines.
The assembler does not recognize that flag so it fails.
I only want the flag to appear in the compiler command line, not the assembler.
How do I do this?
set_source_files_properties(${SOURCES_C} PROPERTIES COMPILE_FLAGS ${TARGET_C_FLAGS})
This appears to work, applying the flags only to those files in the SOURCES_C variable.
Related
I have found the way to configure according to Andre's comment in How to change a compiler flag for just one executable in CMake? .
For example, I add a flag for bubble.c:
set_source_files_properties( ${ProjDirPath}/../bubble.c PROPERTIES COMPILE_FLAGS "-O2")
It worked.But I can't specify the flag for debug or release.
Dose anyone know how to configure it? Thank you!
IF(CMAKE_BUILD_TYPE MATCHES DEBUG)
set_source_files_properties( ${ProjDirPath}/../bubble.c PROPERTIES COMPILE_FLAGS "-O2")
ENDIF(CMAKE_BUILD_TYPE MATCHES DEBUG)
Use special syntax like this:
set_source_files_properties(myfile.cpp PROPERTIES
COMPILE_FLAGS $<$<CONFIG:RelDebug>:/O2>
SKIP_PRECOMPILE_HEADERS $<$<CONFIG:RelDebug>:ON>
)
Sets specific compilation option only to specific configuration (in this case for RelDebug).
I would like to compile the cmake file from Manyears 1.1.2 sources, but I got the errors as below:
WARNING : manyears GUI will not be compiled because Qt4 not found
-- Buiding ManyEarsLib Library...
CMake Error at dsplib/CMakeLists.txt:75 (set_target_properties):
set_target_properties called with incorrect number of arguments.
-- Buiding RTAudio Library...
CMake Error at example/CMakeLists.txt:22 (set_target_properties):
set_target_properties called with illegal arguments, maybe missing a
PROPERTIES specifier?
And the original program has been written as below:
target_link_libraries(ManyEarsLib )
set_target_properties(ManyEarsLib PROPERTIES LINK_FLAGS ${MANYEARS_LINK_FLAGS} OUTPUT_NAME man-years)
add_executable(manyears_console manyears_console.c)
set_target_properties(manyears_console PROPERTIES LINK_FLAGS ${MANYEARS_LINK_FLAGS})
target_link_libraries(manyears_console ManyEarsLib -lm)
Could somebody tell me what is the problem and how to risolve it?
Thanks a lot.
Lun
Command set_target_properties may only set single-value properties.
But you attempt to set property LINK_FLAGS, which is generally multi-value (a list), but in your case it is simply empty. That is why first invocation detects incorrect number of arguments (it should be even), and the second invocation detects insufficient number of arguments (is should be 4 at least).
For set multi-value properties or for clear them use command set_property:
# Multi-value (or empty) property
set_property(TARGET ManyEarsLib PROPERTY LINK_FLAGS ${MANYEARS_LINK_FLAGS})
# Single-value property
set_target_properties(ManyEarsLib PROPERTIES OUTPUT_NAME man-years)
# Multi-value (or empty) property again
set_property(TARGET manyears_console PROPERTY LINK_FLAGS ${MANYEARS_LINK_FLAGS})
or even
# Multi-value (or empty) property for several targets
set_property(TARGET ManyEarsLib manyears_console PROPERTY LINK_FLAGS ${MANYEARS_LINK_FLAGS})
# Single-value property for single target
set_target_properties(ManyEarsLib PROPERTIES OUTPUT_NAME man-years)
I'm trying to debug a problem related to compiler optimisation (no issue with -O2 or below, segfault with -O3) and I'd like to be able to switch the compiler flags for a chunk of my source so I can try to narrow down where the segfault is coming from.
I can do this setting the global optimisation level to -O2, and altering the PROPERTIES for single files like so:
SET_SOURCE_FILES_PROPERTIES(file1.f90 PROPERTIES COMPILE_FLAGS -O3)
However, when I try to do this for multiple files using *.f90 for example, it seems to not work:
SET_SOURCE_FILES_PROPERTIES(*.f90 PROPERTIES COMPILE_FLAGS -O3)
Is there any way to do this for multiple files without specifying every file by name?
You can glob for a list of files:
file(GLOB MyFiles *.f90)
set_property(SOURCE ${MyFiles} PROPERTY COMPILE_FLAGS -O3)
Alternatively, you could set the COMPILE_FLAGS target property of the respective target instead. Usually, it does not make much sense to compile certain source files with different compile flags than others within the same target. So unless you have good reason to do this on a per-file basis, you should always use the target properties instead.
Just adding to #ComicSansMS correct answer.
You could also use SET_SOURCE_FILES_PROPERTIES this way:
file(GLOB MyFiles *.f90)
SET_SOURCE_FILES_PROPERTIES(${MyFiles} PROPERTIES COMPILE_FLAGS -O3)
This code will have the same effect as
file(GLOB MyFiles *.f90)
set_property(SOURCE ${MyFiles} PROPERTY COMPILE_FLAGS -O3)
What is the difference between
COMPILE_FLAGS: Additional flags to use when compiling this target's sources.
and
COMPILE_OPTIONS: List of options to pass to the compiler.
In terms of resulting VS2010 solution these commands produce the same result:
target_compile_options(target PRIVATE "/option=test1")
set_target_properties(target PROPERTIES COMPILE_FLAGS "/option=test1")
set_target_properties(target PROPERTIES COMPILE_OPTIONS "/option=test1")
COMPILE_OPTIONS is a list, but COMPILE_FLAGS is a string.
set_target_properties(target PROPERTIES
COMPILE_OPTIONS "/option=test1;/option2=test2")
set_target_properties(target PROPERTIES
COMPILE_FLAGS "/option=test1 /option2=test2")
You can more-easily append to a list than to a string.
Also, COMPILE_OPTIONS is properly escaped, whereas some characters in COMPILE_FLAGS may need to be escaped manually or cause problems.
Can be used to the same end, but flags are associated with some target enviroment. So you could use different sets of flags for different enviroments.
SET_TARGET_PROPERTIES(
wtdbo
PROPERTIES
VERSION ${VERSION_SERIES}.${VERSION_MAJOR}.${VERSION_MINOR}
SOVERSION ${WTDBO_SOVERSION}
DEBUG_POSTFIX "d"
)
The error is:
CMake Error at src/Wt/Dbo/CMakeLists.txt:18 (SET_TARGET_PROPERTIES):
set_target_properties called with incorrect number of arguments
If I remove it it configures just fine.
Any idea why?
Thanks,
Omer
Remember that this is a macro, so symbols are replaced before being evaluated. This means that symbols that are empty strings will be replaced to nothing before being evaluated. Thus, if WTDBO_SOVERSION is "" then
SET_TARGET_PROPERTIES(wtdbo PROPERTIES SOVERSION ${WTDBO_SOVERSION})
would become
SET_TARGET_PROPERTIES(wtdbo PROPERTIES SOVERSION)
and this would trigger the error. If empty strings are valid for your purpose then surround the symbol in quotes. e.g.
SET_TARGET_PROPERTIES(wtdbo PROPERTIES SOVERSION "${WTDBO_SOVERSION}")
Are you sure you have the variables set correctly? I've checked with this CMakeLists.txt file, and it works correctly:
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(test CXX)
ADD_LIBRARY(wtdbo SHARED test.cc)
SET(WTDBO_SOVERSION 1)
SET(VERSION_SERIES 1)
SET(VERSION_MAJOR 0)
SET(VERSION_MINOR 0)
SET_TARGET_PROPERTIES(
wtdbo
PROPERTIES
VERSION ${VERSION_SERIES}.${VERSION_MAJOR}.${VERSION_MINOR}
SOVERSION ${WTDBO_SOVERSION}
DEBUG_POSTFIX "d"
)
However, if I comment out the SET(WTDBO_SOVERSION 1) line I get the same error message as you do. The help for set_target_properties is as follows, so you are definitely doing the right thing:
Targets can have properties that
affect how they are built.
set_target_properties(target1 target2 ...
PROPERTIES prop1 value1
prop2 value2 ...)
Set properties on a target. The
syntax for the command is to list all
the files you want to change, and then
provide the values you want to set
next. You can use any prop value pair
you want and extract it later with the
GET_TARGET_PROPERTY command.