Link test target to shared plugin code in CMake build

I’m using juce_add_plugin to build my plugin with CMake. I noticed this creates an artefact called libProjectNameSharedCode.a which I take to be a static library that is used when building the various plugin format binaries.

I have a target that builds my unit tests using juce_add_console_app(Test), and I would like to link the shared library to this target as well.

I have tried:

target_link_libraries(Test
    PRIVATE
        ../pathToLibrary/libProjectName_SharedCode.a

But get the error:

ld: library not found for -l../pathToLibrary/libProjectName_SharedCode.a

Is there a clever way to do this?

Yes there is a clever way :wink:

In target_link_libraries you can also specify another CMake target – if this target generates a library it will link to it. This also ensures that the specified target will be built before you attempt to link against it, which is not guaranteed with your approach. And it propagates all the PUBLIC or INTERFACE properties set to the library target like e.g. header search paths, compiler flags etc to the target that links against it.

juce_add_plugin(my_plugin) creates multiple targets, which are my_plugin (building libmy_plugin_sharedCode.a on unix or my_plugin_sharedCode.lib on Windows) and one for each plugin format, named e.g. my_plugin_vst3.

So all you want for a cross platform test target is

target_link_libraries(Test
    PRIVATE
        my_plugin)

It’s similar to when you were using FRUT.
You were writing

target_link_libraries(Test PRIVATE ProjectName_Shared_Code)

and now with JUCE 6 you need to write:

target_link_libraries(Test PRIVATE ProjectName)

Thanks, that works.

However, I get a cr*p ton of warnings about the JUCE_STANDALONE_APPLICATION macro being redefined.

When building stanalone plugin:

[ 50%] Generating Plugin_artefacts/JuceLibraryCode/Plugin_Standalone/PkgInfo
In file included from In file included from In file included from <built-in><built-in><built-in>:::400400:
:
400:
<command line><command line>::In file included from In file included from <command line>1515<built-in>::<built-in>99:::::In file included from 40040015  :
:
<built-in>warning:warning: 9<command line><command line>:::15: 'JUCE_STANDALONE_APPLICATION' macro redefined [-Wmacro-redefined]: 400warning:
:15
'JUCE_STANDALONE_APPLICATION' macro redefined [-Wmacro-redefined]: :99:<command line>
: : 15:9warning: :warning'JUCE_STANDALONE_APPLICATION' macro redefined [-Wmacro-redefined]:  'JUCE_STANDALONE_APPLICATION' macro redefined [-Wmacro-redefined]
'JUCE_STANDALONE_APPLICATION' macro redefined [-Wmacro-redefined]
warning: 
'JUCE_STANDALONE_APPLICATION' macro redefined [-Wmacro-redefined]
#define JUCE_STANDALONE_APPLICATION JucePlugin_Build_Standalone#define JUCE_STANDALONE_APPLICATION JucePlugin_Build_Standalone

        ^        ^

<command line><command line>:#define JUCE_STANDALONE_APPLICATION JucePlugin_Build_Standalone:14
14::9        ^#define JUCE_STANDALONE_APPLICATION JucePlugin_Build_Standalone
9:
 : #define JUCE_STANDALONE_APPLICATION JucePlugin_Build_Standalone#define JUCE_STANDALONE_APPLICATION JucePlugin_Build_Standalone

        ^<command line>        ^        ^:

note
14: note: :previous definition is hereprevious definition is here

9<command line>:: 14:9:note : previous definition is herenote: 
previous definition is here
#define JUCE_STANDALONE_APPLICATION 1
        ^<command line>#define JUCE_STANDALONE_APPLICATION 1
:
<command line>14::        ^149
::9 : note#define JUCE_STANDALONE_APPLICATION 1: 
note#define JUCE_STANDALONE_APPLICATION 1        ^: 

previous definition is hereprevious definition is here        ^


#define JUCE_STANDALONE_APPLICATION 1
        ^
#define JUCE_STANDALONE_APPLICATION 1
        ^ 

And for each of the test test source files:

[ 64%] Building CXX object test/CMakeFiles/Test.dir/modules/mono_step/state/MonoStepStateTests.cpp.o
In file included from <built-in>:400:
<command line>:15:9: warning: 'JUCE_STANDALONE_APPLICATION' macro redefined [-Wmacro-redefined]
#define JUCE_STANDALONE_APPLICATION JucePlugin_Build_Standalone
        ^
<command line>:14:9: note: previous definition is here
#define JUCE_STANDALONE_APPLICATION 1
        ^

Pretty cryptic with all those references to line numbers on the command line.

Any idea what might be causing these warnings?

This comes from using juce_add_console_app. The JUCE settings are then defined twice: once from juce_add_console_app and once from linking against the plugin.

You should try using a regular add_executable(Test) to create the test executable.

I hope this helps!

Makes sense, thanks!