<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Hello,<br>
    <br>
    I am linking my code to a library. My code is in C++ but the library
    is in C.<br>
    <br>
    The problem is that whenever the extension of library
    implementations are "<font face="Courier New, Courier, monospace">c</font>"
    (not <font face="Courier New, Courier, monospace">cpp</font>), they
    are not linked and I get "<font face="Courier New, Courier,
      monospace">undefined reference to</font>" error.<br>
    <br>
    Here is an example:<br>
    <br>
    Suppose I have<br>
    <font face="Courier New, Courier, monospace">./src/main.cpp</font>  
    // as main file<br>
    <font face="Courier New, Courier, monospace">./include/lib.h</font><br>
    <font face="Courier New, Courier, monospace">./include/lib.c</font> 
    // as a library<br>
    <br>
    And the cmake file is<br>
    <br>
    <font face="Courier New, Courier, monospace">cmake_minimum_required(VERSION
      2.8)<br>
      project(myproj)<br>
      <br>
      set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)<br>
      include_directories(${INCLUDE_DIR})</font><br>
    <font face="Courier New, Courier, monospace"><font face="Courier
        New, Courier, monospace">add_library(MY_LIB
        ${INCLUDE_DIR}/Lib.c)<br>
        <br>
      </font>set(EXECUTABLE_NAME "myproj")<br>
      set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)<br>
      add_executable(myproj ${SOURCE_DIR}/main.cpp)<br>
      <br>
      target_link_libraries(myproj MY_LIB)</font><br>
    <br>
    This returns <font face="Courier New, Courier, monospace">undefined
      reference to</font> error, but if I rename <font face="Courier
      New, Courier, monospace">lib.c</font> to <font face="Courier New,
      Courier, monospace">lib.cpp</font>, everything works fine.<br>
    <br>
    How can I resolve this? I do not want to rename the file to <font
      face="Courier New, Courier, monospace">cpp</font>, because there
    are a lot of library files and I prefer to keep the library
    implementations untouched.<br>
    <br>
    Thanks.
  </body>
</html>