I have 3 files with me, one c++ file, main.cpp, one c file, test.c and one header file, test.h
I wanted to try and use C code into C++ file. For the same reason, I have declared an function in test.h and defined that in test.c and using that in main.cpp
main_temp.c is just for explanation.
test.h
void test(int); test.c
#include <stdio.h> void test(int a) { printf("%d", a); main_temp.cpp
#include "test.h" int main() { foo(5); } Here, I understand why this would not work. C symbol would be simple 'foo' but since C++ does more things while creating symbols, it might be 'void@test(int)' and to solve this name mangling problem, I have to treat C++ symbol as a C symbol. So, I would use extern "C" and my main.cpp becomes as like:
main.cpp
extern "C" { #include "test.h" } int main() { foo(5); } I could not understand as to why this would not work! I get :
main.cpp:(.text+0xa): undefined reference to `test` Can somebody share the insights?
Makefile, orCMakefile, or etc. if you're using a build system.test.ctestreally is missing.g++frontend to compile C files. You need to compile test.c withgccand main.cpp withg++and then link them together.