我正在用CMake構建C++應用程序。但它使用了一些源文件中C.這裏是簡化的結構:在CMake中鏈接C和CXX文件
軀幹/的CMakeLists.txt:
project(myapp)
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -g -Wall")
add_subdirectory (src myapp)
軀幹/ SRC/main.cpp中:
#include "smth/f.h"
int main() { f(); }
軀幹/ SRC /的CMakeLists.txt:
add_subdirectory (smth)
link_directories (smth)
set(APP_SRC main)
add_executable (myapp ${APP_SRC})
target_link_libraries (myapp smth)
主幹/ src目錄/水木清華/ FH:
#ifndef F_H
#define F_H
void f();
#endif
主幹/ src目錄/水木清華/ FC:
#include "f.h"
void f() {}
主幹/ src目錄/水木清華/的CMakeLists.txt
set (SMTH_SRC some_cpp_file1 some_cpp_file2 f)
add_library (smth STATIC ${SMTH_SRC})
的問題是:我運行gmake命令,它編譯所有的文件和當它鏈接所有庫在一起,我得到:
undefined reference to `f()` in main.cpp
如果我重命名fc到f.cpp一切都很好。有什麼區別以及如何處理它?
感謝
關於此問題的詳細介紹:'混合使用C和C++:'http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html – Lazer 2010-03-08 12:36:52