2010-03-08 4 views
7

Ich baue C++ App mit CMake. Aber es nutzt einige Quelldateien in C ist hier vereinfachte Struktur:Verknüpfen von C- und CXX-Dateien in CMake

Stamm/CMakeLists.txt:

project(myapp) 
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -g -Wall") 
add_subdirectory (src myapp) 

trunk/src/main.cpp:

#include "smth/f.h" 
int main() { f(); } 

trunk/src/CMakeLists.txt:

add_subdirectory (smth) 
link_directories (smth)  
set(APP_SRC main) 
add_executable (myapp ${APP_SRC}) 
target_link_libraries (myapp smth) 

trunk/src/smth/fh:

#ifndef F_H 
#define F_H 
void f(); 
#endif 

trunk/src/smth/fc:

#include "f.h" 
void f() {} 

trunk/src/smth/CMakeLists.txt

set (SMTH_SRC some_cpp_file1 some_cpp_file2 f) 
add_library (smth STATIC ${SMTH_SRC}) 

Das Problem ist: i gmake laufen, es alle Dateien kompiliert und wenn es alle libs miteinander verbindet, erhalte ich:

undefined reference to `f()` in main.cpp 

wenn ich fc in f.cpp alles umbenennen geht ganz gut. Was ist der Unterschied und wie geht man damit um?

Dank

+1

Eine gute Lektüre dazu: 'Mischen von C und C++:' http://www.parashift.com/c++faq-lite/mixing-c-and-cpp.html – Lazer

Antwort

14

ändern f.h zu:

#ifndef F_H 
#define F_H 

#ifdef __cplusplus 
extern "C" { 
#endif 

void f(); 

#ifdef __cplusplus 
} 
#endif 

#endif 
2

Bitte C Wachen auf alle Ihre C-Dateien hinzufügen. Weitere Informationen hierzu finden Sie unter link.