1

Yes, I know there's already several questions that go into detail about this, but I feel like my question isn't that detail-specific. Just something staring at me in the face, but I can't quite see it.

I'm compiling C and C++ files together in a Makefile. Everything seems to be running fine until I get the titular error, which pertains to the functions in CFile2.

Compilation works like this (w/ placeholder names):

g++ -c main.cpp
g++ -c Class.cpp
gcc -c CFile1.c
gcc -c CFile2.c
g++ main.o Class.o CFile1.o CFile2.o -lcurl

My Class.cpp has a Class.hpp, and my CFile1 and CFile2 both have .h files respectively. Everything is located within the same directory, and they all have the same header guard structure.

#ifndef
#define

//Insert function prototypes here

#endif

I'm also only including the CFile1.h and CFile2.h in Class.hpp. Is there something I'm missing based on this info only?

hudspero
  • 139
  • 1
  • 1
  • 9
  • maybe look at this answer: http://stackoverflow.com/questions/16850992/call-a-c-function-from-c-code ... Also, #pragma(once) (you may have to google it) is nicer than those include guards... –  Sep 03 '15 at 21:39
  • #pragma once works if your build environment A) uses a compiler that supports it (most seem to these days, but it fails silently if the build system does not) and B) does not use network mounted drives or symlinks. – user4581301 Sep 03 '15 at 22:01

1 Answers1

1

Maybe you are missing the extern "C" in the c header?

If you compile c file and want to link with c++ you mast add extern "C", but the c compiler doesn't recognize it so it must is the #ifdef __cplusplus

Add at the beginning of the header, before functions deceleration:

#include <stdio> // and all other includes...
#ifdef __cplusplus
extern "C" {
#endif

void funnction_name1(); //sample 1
int funnction_name2(char* p); //sample 2

//must close the extern "C" block
#ifdef __cplusplus
}
#endif

you can also add it before each function as follows:

 #ifdef __cplusplus
 extern "C"
 #endif
 // no block for the extern "C", so it applied only for a single function
 void funnction_name(); //a sample func.
SHR
  • 7,940
  • 9
  • 38
  • 57