Add_compile_optings or
Correctly modify through the set command
CMAKE_CXX_FLAGS
or
CMAKE_C_FLAGS
。
Use these two methods to effect the same effect under some cases (note that in some cases, not under all cases), their differences are as follows:
The compile option added by the
add_compile_options
command is for all compilers (including C and C ++ compilers).
set command settingsCMAKE_C_FLAGS
orCMAKE_CXX_FLAGS
variables are only for C and C ++ compilers.
Therefore, in the compilation of the hybrid program of C/C ++ and OC, when setting “-X Objective-C ++”, it can only be usedadd_compile_optings to add compilation options, otherwise, C/C ++ will report inexplicable mistakes when compiling the OC code.
So, when C/C ++ and OC are mixed with compile, the cmakelists.txt should be written as follows:
add_compile_options(-x objective-c++)
set(CMAKE_EXE_LINKER_FLAGS “-framework Cocoa -framework AppKit -framework CoreData -framework Foundation”)
The corresponding G ++ command line parameters are as follows:
More than
g++ -x objective-c++ -framework Cocoa -framework AppKit -framework CoreData -framework Foundation
, compile, can successfully produce executable files.