Linking C and CXX files in CMake

Posted by vedro so snegom on Stack Overflow See other posts from Stack Overflow or by vedro so snegom
Published on 2010-03-08T10:57:51Z Indexed on 2010/03/08 11:06 UTC
Read the original article Hit count: 494

Filed under:
|
|
|

Hi

I'm building C++ app with CMake. But it uses some source files in C. Here is simplified structure:

trunk/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/f.h:

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

trunk/src/smth/f.c:

#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})

The problem is: i run gmake, it compiles all the files and when it links all libs together, i get:

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

if i rename f.c into f.cpp everything goes just fine. What's the difference and how to handle it?

Thanks

© Stack Overflow or respective owner

Related posts about cmake

Related posts about c