[Cmake] How to force linking with C++ compiler given C code?

Douglas Gregor gregod at cs . rpi . edu
Thu, 18 Dec 2003 13:25:07 -0500


Greetings,
	I'm trying to link an executable (compiled entirely from C sources) to a C++ 
library using CMake under Linux. When the library is a shared library, there 
are no problems (of course). When trying to link statically, the generated 
makefile uses "gcc" instead of "g++", causing undefined symbols when 
referring to, e.g., std::cout.
	Here's one way to reproduce the problem (CMake 1.8.1, GCC 3.2.3, glibc 
2.3.2):

------------CMakeLists.txt is:
ADD_LIBRARY(hello_library hello.cpp)
ADD_EXECUTABLE(hello_prog main.c)
TARGET_LINK_LIBRARIES(hello_prog hello_library)

------------hello.cpp is:
#include <iostream>
extern "C" {
  void hello() { std::cout << "Hello, World!" << std::endl; }
}

------------main.c is:
void hello();

int main()
{
  hello();
  return 0;
}

	Is there any way to request that hello_prog be linked as if it were C++ code?	

	Thanks,
	Doug