JUCE linking issues with a CMake library that links to JUCE with a test project executable

I have a library that includes some JUCE functionality. This library links to JUCE (specifically the juce core module). Here’s the CMakeLists.txt for this:

cmake_minimum_required(VERSION 3.12)

project(salsa LANGUAGES CXX)

option(BUILD_TESTS "Build tests" ON)

add_subdirectory(dependencies/JUCE)

add_library(salsa SHARED 
    src/MyAudioCode.cpp
)

target_include_directories(salsa PUBLIC
    include
)

set_target_properties(salsa PROPERTIES
    LINKER_LANGUAGE CXX
    CXX_STANDARD 20
    CXX_STANDARD_REQUIRED YES
)

target_link_libraries(salsa PRIVATE
    juce::juce_core
)

if(BUILD_TESTS)
   enable_testing()
   add_subdirectory(test)
endif()

This all works fine, the issue is is that we have a separate CMakeLists project which is a test executable using googletest. Here’s the CMakeLists.txt for that:

#Add google test and set it not to build googlemock
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
set(gtest_disable_pthreads ON CACHE BOOL "" FORCE)
add_subdirectory(googletest)

add_executable(tests 
    AudioTests.cpp
)

target_include_directories(tests PRIVATE 
    ../include
)

set_target_properties(tests PROPERTIES 
    LINKER_LANGUAGE CXX
    CXX_STANDARD 20
    CXX_STANDARD_REQUIRED YES
)

target_link_libraries(tests PRIVATE
    GTest::gtest_main   
    salsa
    juce::juce_core 
)

include(GoogleTest)
gtest_discover_tests(tests)

install(TARGETS tests)

This is where things start to fall over. The test project builds but it crashes as soon as it starts. If I remove the linking to JUCE this stops. Here’s the crash errors:

tests(52470,0x1f0db5e80) malloc: *** error for object 0x102d38300: pointer being freed was not allocated
tests(52470,0x1f0db5e80) malloc: *** set a breakpoint in malloc_error_break to debug

I’ve read a few similar issues that say to link juce modules as interfaces but I haven’t been able to get that working yet. Also changing the test project to use juce_add_executable also doesn’t solve the issue.

You can build salsa as STATIC.

Or keep it shared but hide the symbols via CXX_VISIBILITY_PRESET hidden and VISIBILITY_INLINES_HIDDEN ON but then on your public API set __attribute__((visibility("default"))).

1 Like

Building as STATIC works, thanks for the help!