<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta content="text/html; charset=utf-8">
</head>
<body>
<div>If you are including the .h file for the .c file in your .cpp file and it doesn't already have extern "C" in it, wrapping it like this when including it in your cpp should help:</div>
<div><br>
</div>
<div>extern "C"</div>
<div>{</div>
<div>#include "header.h"</div>
<div>}</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<div id="composer_signature">
<div style="font-size:85%; color:#575757">Sent from my Verizon Wireless 4G LTE smartphone</div>
</div>
<br>
<br>
-------- Original message --------<br>
From: J Decker <d3ck0r@gmail.com> <br>
Date: 06/05/2015 10:33 PM (GMT-05:00) <br>
To: Sunrise <helios.corona@gmail.com> <br>
Cc: cmake@cmake.org <br>
Subject: Re: [CMake] undefined reference to error when use "c" extension <br>
<br>
<div>
<div dir="ltr">c++ does name mangling on functions... so functions like 'f' become a much more complex name (as shown in the xxx not found in your error messages).
<div>In order for C++ to not produce a mangled name C functions have to be defined as </div>
<div><br>
</div>
<div>extern "c" void f( void ); </div>
<div>but 'extern "c"' is not liked by C... so you really need to define in the shared header something like...</div>
<div><br>
</div>
<div>#ifdef __cplusplus</div>
<div>#define CEXTERN extern "C"</div>
<div>#ese</div>
<div>#define CEXTERN </div>
<div>#endif</div>
<div><br>
</div>
<div>CEXTERN void f( void );</div>
<div><br>
</div>
<div>But of course since you don't know about name mangling I guess you don't know proper header usage either.  This is not a cmake issue, but a general C++ issue... and you'd do better asking stack exchange or something.</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Fri, Jun 5, 2015 at 6:37 PM, Sunrise <span dir="ltr">
<<a href="mailto:helios.corona@gmail.com" target="_blank">helios.corona@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<div bgcolor="#FFFFFF">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. </div>
<br>
--<br>
<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" target="_blank">
http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" target="_blank">
http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" target="_blank">
http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" target="_blank">
http://cmake.org/cmake/help/training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">
http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/cmake" target="_blank">http://public.kitware.com/mailman/listinfo/cmake</a><br>
</blockquote>
</div>
<br>
</div>
</div>
</body>
</html>