Compile C++ files with Post-Build Shell Script

I am trying to use some C++ code in my JUCE project which has a number of cpp files, a compile.sh file and two shell scripts for running the code.

Would it be possible to run compile.sh as a Post-Build Shell Script and then run the executable files from my JUCE project using ChildProcess and the two shell scripts?

Ideally, I would like to simply download the folder and include it in my JUCE project folder, create the necessary executables at compile time and access them at run time. However, I am not sure if this is possible or where the executables will be stored.

This is messy and prone to clumsiness.

A better solution would be to make a library out of the files in the project, submit a PR to the original authors, and then integrate that library in your JUCE project.

Shouldn’t be too hard - would just need to use some kind of macro to guard the inclusion of main(), add the .cpp files to an EXTRA_SOURCES-style definition in your JUCE Project, and then just build it in.

(EDIT: removed incorrect assessment of sources that could be used as modules)

… so it really depends what functionality you want to include in your JUCE project - it may very well be the case that you can simply include the AlignmentTool project sources as a submodule in your main JUCE project, and compile/link the relevant sources directly.

AlignmentTool is a very interesting project, btw! Would be cool to see it turned into a more functional library that other JUCE devs could include …

Thanks for your suggestions.

I took your advice and started exploring how best to make a library out of the files in the project. Since all the .cpp files include a main() function I decided to add them as executables to a CMakeLists.txt file in the AlignmentTool folder.

add_executable(ErrorDetection Code/ErrorDetection_v190702.cpp)
add_executable(RealignmentMOHMM Code/RealignmentMOHMM_v170427.cpp)
add_executable(ScorePerfmMatcher Code/ScorePerfmMatcher_v170101_2.cpp)
add_executable(midi2pianoroll Code/midi2pianoroll_v170504.cpp)
add_executable(MusicXMLToFmt3x Code/MusicXMLToFmt3x_v170104.cpp)
add_executable(MusicXMLToHMM Code/MusicXMLToHMM_v170104.cpp)
add_executable(SprToFmt3x Code/SprToFmt3x_v170225.cpp)
add_executable(Fmt3xToHmm Code/Fmt3xToHmm_v170225.cpp)
add_executable(MatchToCorresp Code/MatchToCorresp_v170918.cpp)

Then in the main project folder I can do the following:

cmake_minimum_required(VERSION 3.25)
project(NakamuraAlignmentTool)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(AlignmentTool)

add_executable(NakamuraAlignmentTool main.cpp)

add_dependencies(NakamuraAlignmentTool
        ErrorDetection
        RealignmentMOHMM
        ScorePerfmMatcher
        midi2pianoroll
        MusicXMLToFmt3x
        MusicXMLToHMM
        SprToFmt3x
        Fmt3xToHmm
        MatchToCorresp
        )

Then from main.cpp I can run the executables as in the original shell script:

std::string command{};

command = "./AlignmentTool/midi2pianoroll 0 " + I2;
std::system(command.c_str());

command = "./AlignmentTool/MusicXMLToHMM " + I1 + ".xml" + " " + I1 + "_hmm.txt";
std::system(command.c_str());

command = "./AlignmentTool/MusicXMLToFmt3x " + I1 + ".xml" + " " + I1 + "_fmt3x.txt";
std::system(command.c_str());

command = "./AlignmentTool/ScorePerfmMatcher " + I1 + "_hmm.txt" + " " + I2 + "_spr.txt" + " " + I2 +
          "_pre_match.txt" + " 1.0";
std::system(command.c_str());

command = "./AlignmentTool/ErrorDetection " + I1 + "_fmt3x.txt" + " " + I1 + "_hmm.txt" + " " + I2 +
          "_pre_match.txt" + " " + I2 + "_err_match.txt" + " 0";
std::system(command.c_str());

command = "./AlignmentTool/RealignmentMOHMM " + I1 + "_fmt3x.txt" + " " + I1 + "_hmm.txt" + " " + I2 +
          "_err_match.txt" + " " + I2 + "_realigned_match.txt" + " 0.3";
std::system(command.c_str());

command = "cp " + I2 + "_realigned_match.txt" + " " + I2 + "_match.txt";
std::system(command.c_str());

Obviously this approaches means migrating the project from the Projucer to CMake. Before I do this I was wondering if this seemed like a reasonable approach? Also, I know this is not quite the same as creating a library but was influenced by a this StackOverflow post and decided to try something similar.

If possible could you elaborate on how you might go about using a macro to guard against the inclusion of main() should I want to create a library instead?

I’m glad you are interested in the AlignmentTool! Happy to share the library with other JUCE devs if I find a portable solution.