C++ undefined reference to defined function
Though previous posters covered your particular error, you can get 'Undefined reference' linker errors when attempting to compile C code with g++, if you don't tell the compiler to use C linkage.
For example you should do this in your C header files:
extern "C" {
...
void myfunc(int param);
...
}
To make 'myfunc' available in C++ programs.
If you still also want to use this from C, wrap the extern "C" {
and }
in #ifdef __cplusplus
preprocessor conditionals, like
#ifdef __cplusplus
extern "C" {
#endif
This way, the extern
block will just be “skipped” when using a C compiler.
The declaration and definition of insertLike
are different
In your header file:
void insertLike(const char sentence[], const int lengthTo, const int length,
const char writeTo[]);
In your 'function file':
void insertLike(const char sentence[], const int lengthTo, const int length,
char writeTo[]);
C++ allows function overloading, where you can have multiple functions/methods with the same name, as long as they have different arguments. The argument types are part of the function's signature.
In this case, insertLike
which takes const char*
as its fourth parameter and insertLike
which takes char *
as its fourth parameter are different functions.
You need to compile and link all your source files together:
g++ main.c function_file.c