9

I realize this question has been asked in a number of ways including this very comprehensive answer but I have looked at many and tried fixing my error to no avail.

I am using .cpp and .c files to create a program. I compiled all files with g++, it seemed to have no more linking errors, though it gave me a bunch of C++ errors related to the C syntax. This was the command I used:

g++ -o program main.cpp /data/*.c -l[...libs]

The main.cpp calls functions in the .c files. I then understood that one should not try to compile both .c and .cpp files with one compiler, instead to use gcc for one, g++ for the other, and thereafter to simply link the object files.

So I did this (the .c files are part of a library, and already have .o files)

g++ -c main.cpp
g++ -o program main.o /data/*.o -l[..libs]

But then here I will get "undefined reference to" errors for functions called from main.cpp to the precompiled .c files, errors which I didn't get previously.

Could anyone help? Or do I maybe need to provide more information?

EDIT (a more in depth excerpt of code, I've tried to simplify otherwise this will be impossible to read, but let me know if I still need to add stuff, I'll try to remove unnecessary code):

main.cpp :

#include "header.h"

int main(int argc, char** argv) {
    string s1 = argv[2];
    fn1(s1)
}

header.h

void fn1(string s1)

mycfile.c

#include "header.h"

void fn1(string s1){
    fprintf(stdout, " you entered %s", s1);
    fflush(stdout);
}

ANSWER:

@Smeehey helped me figure out the solution, it was actually that I was still including the old headers in an .hpp file I was using. But the core solution was indeed to use the external C{}.

Community
  • 1
  • 1
Victor.dMdB
  • 999
  • 2
  • 11
  • 29
  • Please post your code, normally undefined ref error is caused when including header files to some code hence getting references to functions / variables and not linking the actual code of the actual functions / definitions – DrPrItay Aug 04 '16 at 08:01
  • Order of arguments to `g++` matters a big lot, even order of libraries. Be more specific. Also, **always** pass `-Wall -g` to `g++` – Basile Starynkevitch Aug 04 '16 at 08:08

3 Answers3

17

This is highly likely to do with C-vs-C++ linkage. In your main.cpp, you probably have something like this:

#include <data/header.h>

where header.h refers to your c library. Replace it as follows:

extern "C" {
#include <data/header.h>
}

This tells your c++ compiler not to use c++-style name mangling when defining the required symbols from the header, allowing the linker to successfully find them in the c-compiled .o files.

Smeeheey
  • 9,906
  • 23
  • 39
  • Just to clarify on this, if I have a function `fn1( string st1)` , which is defined in a .c file, declared with an `include header.h` in my .cpp file, and i DON'T use `extern C`, when I check the `nm -u` I will get something like `U _Z16fn1iPPcP7String`. while if i DO use `extern C`, I should get something like `U fn1`? – Victor.dMdB Aug 04 '16 at 09:38
  • 1
    When running `nm` on your *c++* compiled object file - yes, that's right. – Smeeheey Aug 04 '16 at 09:40
  • Yes on the C++ .o file. hmm that might be the cause of the issue then, even after I add the 'extern C' i still get the mangled name – Victor.dMdB Aug 04 '16 at 09:42
  • 1
    Can you post your full code, command lines, and linker error? – Smeeheey Aug 04 '16 at 09:44
  • I could do that but it's currently very large and unwieldy (main.cpp is 450 lines) since I've yet to clean it up... The command line is `g++ -Wall -v -std=c++11 -c src/main.cpp -o main.o -I include/ -I /usr/include/gpac/applications/dashcast/` – Victor.dMdB Aug 04 '16 at 09:57
  • In main I have: `extern "C" { #include #include } int main(int argc, char** argv) { dc_parse_command(argc, argv, &cmd_data); dc_run_controler(&cmd_data); dc_cmd_data_destroy(&cmd_data);}` – Victor.dMdB Aug 04 '16 at 10:00
  • And the the sourcecode for dashcast is [here] (https://github.com/gpac/gpac/tree/master/applications/dashcast) – Victor.dMdB Aug 04 '16 at 10:02
  • And which symbol from the header files is still mangled according to your `nm -u` output? – Smeeheey Aug 04 '16 at 10:04
  • All 3 of those commands – Victor.dMdB Aug 04 '16 at 10:14
  • 1
    Are you getting this issue with your example code or your actual code? I can trivially show that mangling is removed in my own version of the example code. Your example code couldn't possibly work (with `extern "C"`) as you pass a `string` to your function `fn` which I assume is an `std::string` - but this couldn't work in a `extern C` header as its a c++ template (unless you used some non-trivial trickery) – Smeeheey Aug 04 '16 at 12:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120137/discussion-between-victor-dmdb-and-smeeheey). – Victor.dMdB Aug 04 '16 at 14:32
2

You have to compile C files with the gcc command, C++ files with either gcc or g++, and link with the g++ command. This sequence will probably work:

 gcc -c data/*.c main.cpp
 g++ -o program *.o -l <libs...>

Next step: learn to write proper makefiles.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0

This is a shot in the dark but the problem might be the way C and Cpp files are compiled is simillar yet slightly different.... Due to name spaces the function foo would generate the symbol some_prefix@foo Unlike C whereas goo generates the symbol goo

Try doing the following in your .cpp files

extern "C" { #include "yourcfilesheader.h" }

And please attach your code

DrPrItay
  • 793
  • 6
  • 20