How to compile multiple .cpp files?

Hi guys,

Hopefully this isn’t really a stupid question, but bear with me!

I’m using VSCode to develop my Daisy code in C++, and uploading via USB using the makefile. I’m used to being able to separate my code into various .cpp files, with associated .h header files containing forward declarations. I would then #include these header files in the primary .cpp file. However when I attempt to compile a program with this structure, I get errors saying I have undefined references to the functions in my additional .cpp files. If I put copy the function into the header file, it works, so it’s as if the compiler is not seeing the additional .cpp files.

I noticed in the makefile there’s the line:

CPP_SOURCES = Example.cpp

Is there a way to compile all .cpp files in the directory? Or am I going about this wrong way? How are other people structuring their directories?

Thanks in advance!

1 Like

Try adding all source code files to that line like so:

CPP_SOURCES = Example.cpp AdditionalFile1.cpp AdditionalFile2.cpp

That gives me the same errors, still complaining about undefined references to the function in the additional .cpp file


CPP_SOURCES = Example.cpp
CPP_SOURCES += AdditionalFile1.cpp
CPP_SOURCES += AdditionalFile2.cpp

There’s also vpath directive that could be used for finding all sources

Oh wait, sorry I think I’ve found the problem. My function forward declaration (in my header file) was inside

namespace daisysp{ },

and I had the line:

using namespace daisysp;

at the top of my additional .cpp file. Removing those and referencing both .cpp files in the makefile has done the trick. Not quite sure why this fixed it, but it now works…

I’ve run into the same problem with trying to link multiple files. I got past it by adding source files for Make as suggested. I ended up replacing the CPP_SOURCES assignment in my Makefile with:

CPP_SOURCES = $(wildcard *.cpp)

This pulls in all .cpp files in the project directory. Unless folks are in the habit of allowing unused .cpp files within a project (I don’t), this seems like a good way to go.

1 Like

Is there a simple example in the codebase on how to do this effectively? or can you link to a working code sample please?