[CMake] undefined reference to error when use "c" extension

Sunrise helios.corona at gmail.com
Fri Jun 5 21:37:02 EDT 2015


Hello,

I am linking my code to a library. My code is in C++ but the library is 
in C.

The problem is that whenever the extension of library implementations 
are "c" (not cpp), they are not linked and I get "undefined reference 
to" error.

Here is an example:

Suppose I have
./src/main.cpp // as main file
./include/lib.h
./include/lib.c // as a library

And the cmake file is

cmake_minimum_required(VERSION 2.8)
project(myproj)

set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${INCLUDE_DIR})
add_library(MY_LIB ${INCLUDE_DIR}/Lib.c)

set(EXECUTABLE_NAME "myproj")
set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
add_executable(myproj ${SOURCE_DIR}/main.cpp)

target_link_libraries(myproj MY_LIB)

This returns undefined reference to error, but if I rename lib.c to 
lib.cpp, everything works fine.

How can I resolve this? I do not want to rename the file to cpp, because 
there are a lot of library files and I prefer to keep the library 
implementations untouched.

Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150605/e8524c1b/attachment.html>


More information about the CMake mailing list