我正在尝试将生成的thrift
代码编译到我的源代码中。将thrift
的东西生成到CMAKE_CURRENT_BINARY_DIR
中,然后使用SET_SOURCE_FILES_PROPERTIES(${ThriftGeneratedFiles} PROPERTIES
GENERATED 1
COMPILE_OPTIONS ""
)
将生成的cpp文件添加到我的库中
然后,我将TARGET_INCLUDE_DIRECTORIES
与SYSTEM
关键字一起添加到生成文件的包含路径。当我尝试对其进行编译时,由于我的模块使用-Wsuggest-override
进行编译,因此无法使用can be marked override [-Werror=suggest-override]
来编译生成的cpp文件。我不明白为什么,SET_SOURCE_FILES_PROPERTIES
是否可以解决此问题?
CMake文件摘要
ADD_COMPILE_OPTIONS(
...
...
-Wsuggest-override
...
...
)
FIND_PROGRAM(THRIFT_COMPILER thrift ${_VCPKG_ROOT_DIR}/buildtrees/thrift/x64-linux-rel/compiler/cpp/bin/ NO_DEFAULT_PATH)
SET(ThriftOutputDir ${CMAKE_CURRENT_BINARY_DIR}/thrift/gen/)
FILE(MAKE_DIRECTORY ${ThriftOutputDir})
SET(ThriftGeneratedFiles
${ThriftOutputDir}/MyServer.cpp
${ThriftOutputDir}/My_types.cpp
${ThriftOutputDir}/My_constants.cpp
)
ADD_CUSTOM_COMMAND(
OUTPUT
generated.timestamp
COMMAND
${THRIFT_COMPILER} --gen cpp:no_default_operators -out ${ThriftOutputDir} ${CMAKE_SOURCE_DIR}/protocols/My.thrift
WORKING_DIRECTORY
${CMAKE_BINARY_DIR}
COMMENT "Generating source code from Thrift definition")
ADD_CUSTOM_TARGET(RunThriftCompiler
DEPENDS
generated.timestamp
COMMENT "Checking if re-generation is required")
ADD_LIBRARY(MyProject SHARED
${SOURCES}
${ThriftGeneratedFiles}
)
SET_SOURCE_FILES_PROPERTIES(${ThriftGeneratedFiles} PROPERTIES
GENERATED 1
COMPILE_OPTIONS ""
)
TARGET_INCLUDE_DIRECTORIES(MyProject SYSTEM PRIVATE
${PROTOBUF_INCLUDE_DIR}
${ThriftOutputDir}
)
编译消息:
cd / home / user / Development / Project / Project16 / cmake-build-debug / lib / MyProject && / usr / bin / c ++ -DCARES_STATICLIB -DMyProject_EXPORTS -I / home / user / Development / Project / Project16 / lib / include / MyProject -I / home / user / Development / Project / Project16 / lib / include -isystem / home / user / Development / Project / Project16 / cmake-build -debug / ext / etcd -isystem / home / user / Development / Project / Project16 / cmake-build-debug / protocols -isystem / home / user / Development / Project / Project16 / ext / spdk / include -isystem / home / user / Development / vcpkg / installed / x64-linux / include -isystem / home / user / Development / Project / Project16 / cmake -build-debug / lib / MyProject / thrift / gen -g -fPIC -include MyProject.h -fPIC -fstrict-aliasing -ffunction-sections -fno-omit-frame-pointer -mtune = core-avx-i -mavx -march = core-avx-i -fno-plt- g -fstack-protector-all -fstack-check -rdynamic -Wno-error =未使用-Wno-error =未使用-Wno-error =未使用但设置了参数-Wshadow -Werror =地址-Werror = array-界限-Werror =字符下标-Werror =枚举比较-Werror =隐式int -Werror =隐式函数声明-Werror =注释-Werror = format -Werror = main -Werror = missing-braces -Werror = nonnull- Werror =指针符号-Werror =重新排序-Werror =返回类型-Werror =序列点-Wstrict-aliasing = 1 -Werror = strict-overflow = 1 -Werror = switch -Werror = trigraphs -Werror =未初始化-Werror =未知实用程序-Wunused函数-Wunused标签-Wunused值-Werror = volatile-register-var -Werror = Clobbered -Werror = empty-body -Werror = ignored-qualifiers -Werror = sign-compare -Werror = type-限制-Werror -Wempty-body -Wuninitialized -Winit-self -Wmissing-clarifications -Wswitch-bool -Wologic-n ot括号-Wsizeof-array-Argument -Wbool-compare -Wtype-limits -Waddress -Wmiseering-indentation -Wshift-negativevalue -Wtautologic-compare -Wnull-dereference -Wduplicated-cond -Wnonnull -Wnonnull-Compare -Wignored-限定词-Wmissing-括号-Wmissing-include-dirs-括号-Wsequence-point -Wno-return-local-addr -Wreturn-type -Wtrigraphs -Wunused-local-typedefs -Winvalid-memory-model -Wmaybe-uninitialized -Wunknown-实用程序-Wframe地址-Wtrampolines -Wfloat-等于-Wno-free-nonheap对象-Wold-style-cast -Wunused参数-Wunused -Wunused-but-set参数-Wframe-大于= 1100000 -Wpointer- arith -Wwrite-strings -Wclobbered -Wenum-compare -Wsizeof-pointer-memaccess -Wmemset-transposed-args -Wologic-op -Wredundant-decls -Winvalid-pch -Wvarargs -Wvector-operation-performance -Wvolatile-register-var- Wdisabled-optimization -Wcast-align -Wcast-qual -Wnon-virtual-dtor -Woverloaded-virtual -Wodr -Wplacement-new = 2 -Wdelete-complete-Wsize-deallocation -Wno-virtual-m ove-assign -Wsuggest-override -DBOOST_COROUTINES_NO_DEPRECATION_WARNING -DGTEST_LINKED_AS_SHARED_LIBRARY -D_GNU_SOURCE -DIGNORE_VALIDATION = -std = gnu ++ 1z -o CMakeFiles / MyProject.dir / thrift / gen / MyProject。 /home/user/Development/Project/Project16/cmake-build-debug/lib/MyProject/thrift/gen/MyProject_constants.cpp 在包含的文件中 /home/user/Development/Project/Project16/cmake-build-debug/lib/MyProject/thrift/gen/MyProject_types.cpp:7:0: /home/user/Development/Project/Project16/cmake-build-debug/lib/MyProject/thrift/gen/MyProject_types.h:72:15: 错误:‘虚拟const char * MyProject :: ThriftCapiException :: what() const”可以标记为覆盖[-Werror = suggest-override] const char * what()const throw();
如图所示,所有编译标志都已应用。
答案 0 :(得分:1)
源文件上的属性,例如COMPILE_DEFINITIONS
和COMPILE_FLAGS
只能添加一个编译选项。使用这些属性,不可能覆盖(删除)为目标编译选项。 (并且ADD_COMPILE_OPTIONS
添加的选项最终分配给了目标。)
您可以从节俭生成的源创建OBJECT
库。您可以为该库设置(即清除)COMPILE_OPTIONS属性。然后可以在主库中使用生成的目标文件:
# Object files for thrift-generated sources
add_library(thriftGenerated OBJECT ${ThriftGeneratedFiles})
# For these files clear compile options which has been set before
set_property(TARGET thriftGenerated PROPERTY COMPILE_OPTIONS)
# Then use object files for thrift-generated sources in other library
add_library(MyProject SHARED
${SOURCES}
$<TARGET_OBJECTS:thriftGenerated>
)