Using C code in JUCE project

Hello!

I am trying to integrate quite a bit of c code into my JUCE project. When compiling with no specific target (AU, standalone, VST, etc.) I have no errors. However, when targeting an actual build I’m receiving linker errors like “Undefined symbols for architecture x86_64:” for all the functions that originate in the C code. Is there some extra step I need to do, other than just including the c code in my project and calling it, to get this to work? Is it even possible to build with C code in JUCE?

do you have extern "C" blocks in your C code?

if not, this should be wrapping every C source file (like an ifdef guard does):

// at top of file:
#ifdef __cplusplus
extern "C" {
#endif

// your C code here...

// at bottom of file:
#ifdef __cplusplus
}
#endif

Thanks for the response! I added that to all of the C files, but I’m still getting the same linker errors :(.

Thanks again for your help!

How are you building the C files? Are you including them as part of your JUCE build system, or are you building them separately and linking the JUCE project to the C library?

This project actually started in Matlab, which has a function to export your Matlab code as C++ code in a JUCE project. In Matlab, I included certain C files and called the functions directly. It seems that in the project generation process, the files were added as if I had gone into the projucer project and used the “Add existing files” button to add them to the project. the files are also in the source code and are referenced properly.

So to answer your question I think I’m including them as part of the JUCE build system.

Usually the way to do it is only include “.h” files (and never “.c”) in your code, and then link with the “.c” files by adding all of them to your build system (Projucer/CMake/etc), which would then make the linker aware of those C functions you’re calling.

Is your files really compiled? I mean in Xcode the files can be there browsable but not included as sources in build machinery. In that case you have IIRC that kind of “Undefined symbols" at linking stage. Are you using the Projucer? CMake? Are you using Xcode?

For instance into the Projucer:

In Xcode a file with a target:

And another one without:

Thanks so much for all the tips! I’ve checked/confirmed that all the things you mentioned are consistent in my code, but I’m still getting the “Undefined Symbols” error :(.

Personally for C code (that i want to compile as C++) i use an amalgam.cpp file where all the required C files are included (if possible), then i add this sole file to compilation. An usual approach in JUCE! I don’t think it will solve your problem, but it is much more easy such to avoid the traps then.

1 Like