在條件編譯特定於平臺的代碼後,我收到了multiple definition
鏈接錯誤。與CMake獲取鏈接錯誤
我的項目是佈局是這樣的:
/
|__+ include/
| |__+ native/
| | |__ impl.h
| |
| |__ general.h
|
|__+ src/
|__+ native/
| |__ impl.linux.c
| |__ impl.win32.c
|
|__ general.c
在general.c
文件的頂部:
#if defined(LIBRARY_PLATFORM_LINUX)
#include "native/impl.linux.c"
#elsif defined(LIBRARY_PLATFORM_WIN32)
#include "native/impl.win32.c"
#endif
我爲了檢測操作系統並定義設置自省中的CMake對應的常量。關鍵是,我不想保持在每個目錄一個CMakeLists.txt
文件,所以我乾脆晶片被保護所有.c
文件as suggested in this answer:
file(GLOB_RECURSE LIBRARY_SOURCE_FILES "${PROJECT_SOURCE_DIR}/src/*.c")
顯然,這是什麼原因造成的問題。它似乎在編譯general.c
中的代碼#include
d以及個別的src/native/impl.*.c
文件。
CMakeFiles/lib.dir/src/native/impl.linux.c.o: In function `declared_in_impl_h':
impl.linux.c:(.text+0x0): multiple definition of `declared_in_impl_h'
CMakeFiles/lib.dir/src/general.c.o:general.c:(.text+0x0): first defined here
如何解開這種情況?
對你的'impl。*'文件使用不同的擴展名。 – 2012-03-18 04:59:43