Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Thursday, 23 June 2016

Undefined reference

Most likely culprit is that you've either not defined the function or you included the library with the definition in the wrong order. [source]

In my case it was a bit more insidious - I was the victim of name mangling. Long story short, be extra careful when mixing C and C++. [solution]

The sad thing is I more or less encountered this problem 6 months ago, but I think I have a better understanding of it now.

Tuesday, 17 November 2015

Mixing C and C++

First, use g++ as it can compile C++ AND C - gcc cannot do that. If for whatever reason you need to use gcc, then just make object files to link with using g++ and the cpp files. 

If you're using malloc and get an error like this,
Process.cpp:45:53: error: invalid conversion from void* to char* [-fpermissive]
     char *linkCopy = malloc(strlen(&line[count]) + 1);
 it's because you need to cast the return value in C++ but not C. See this SO Q&A

Linker complains that a function doesn't exist when it does. First verify with nm that the function does indeed exist. You can use it on .o object and .a archive (static library) files. If it does, the your problem is you're trying to include a header for a C file in a C++ file. There's some problems with not enough information being generated from the C file compared to what the C++ file needs. See this SO Q&A for a solution