CMake and Intellisense for Windows VSCode

Heya all! I’ve been trying to set up CMake to replace the ProJucer for an audio plugin, and running into some difficulties with Intellisense for VSCode on Windows. Basically Intellisense is having difficulties finding and linking Juce. The build works fine, so I don’t think it’s an issue with my CMakeList (posted below, it’s just the same as the example really). I was wondering if anyone has found a fix to get intellisense to actually sense intelligently with Juce? I want to be able to inspect different things as I code rather than code somewhat blindly.

I’ve tried forcing the include of the JuceHeader.h file with a forcedInclude, as well as including the JuceLibraryCode/modules in the properties file, but it didn’t fix the issue.

cmake_minimum_required(VERSION 3.22)

# Always use the newest C++ standard on green-field projects if possible.
set(CMAKE_CXX_STANDARD 23)
if (APPLE)
    # On Mac, we need to wait for a new JUCE version that fixes the compilation issue
    set(CMAKE_CXX_STANDARD 20)
endif()

# This is the internal name of the project and the name of JUCE's shared code target
# Note: This cannot have spaces (it may be 2024, but you can't have it all!)
# Worry not, JUCE's PRODUCT_NAME can have spaces (and is what DAWs display)
set(PROJECT_NAME "PluginName")

project(${PROJECT_NAME} VERSION 0.1)

add_subdirectory(JUCE)

juce_add_plugin(${PROJECT_NAME}
    # VERSION ...                               # Set this if the plugin version is different to the project version
    # ICON_BIG ...                              # ICON_* arguments specify a path to an image file to use as an icon for the Standalone
    # ICON_SMALL ...
    # COMPANY_NAME ...                          # Specify the name of the plugin's author
    IS_SYNTH TRUE                               # Is this a synth or an effect?
    NEEDS_MIDI_INPUT TRUE                       # Does the plugin need midi input?
    # NEEDS_MIDI_OUTPUT TRUE/FALSE              # Does the plugin need midi output?
    # IS_MIDI_EFFECT TRUE/FALSE                 # Is this plugin a MIDI effect?
    # EDITOR_WANTS_KEYBOARD_FOCUS TRUE/FALSE    # Does the editor need keyboard focus?
    # COPY_PLUGIN_AFTER_BUILD TRUE/FALSE        # Should the plugin be installed to a default location after building?
    PLUGIN_MANUFACTURER_CODE Qlts               # A four-character manufacturer id with at least one upper-case character
    PLUGIN_CODE Aran                            # A unique four-character plugin id with exactly one upper-case character
                                                # GarageBand 10.3 requires the first letter to be upper-case, and the remaining letters to be lower-case
    FORMATS AU VST3 Standalone                  # The formats to build. Other valid formats are: AAX Unity VST AU AUv3
    PRODUCT_NAME "PluginName"                        # The name of the final executable, which can differ from the target name
)

juce_generate_juce_header(${PROJECT_NAME})

file(GLOB_RECURSE SourceFiles CONFIGURE_DEPENDS 
"${CMAKE_CURRENT_SOURCE_DIR}/Source/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Source/*.h")
target_sources("${PROJECT_NAME}" PRIVATE ${SourceFiles})

target_compile_definitions(${PROJECT_NAME}
    PUBLIC
        # JUCE_WEB_BROWSER and JUCE_USE_CURL would be on by default, but you might not need them.
        JUCE_WEB_BROWSER=0  # If you remove this, add `NEEDS_WEB_BROWSER TRUE` to the `juce_add_plugin` call
        JUCE_USE_CURL=0     # If you remove this, add `NEEDS_CURL TRUE` to the `juce_add_plugin` call
        JUCE_VST3_CAN_REPLACE_VST2=0)


target_link_libraries(${PROJECT_NAME}
    PRIVATE
        juce::juce_audio_utils
    PUBLIC
        juce::juce_recommended_config_flags
        juce::juce_recommended_lto_flags
        juce::juce_recommended_warning_flags)

This is a bug in JUCE. Please vote and comment here to perhaps persuade the team to implement the solution:
https://forum.juce.com/t/fr-add-explicit-includes-to-juce-modules

Now as to a few things you can do today:

  1. You’d get slightly better results by not using JuceHeader and instead include the JUCE modules you’re using directly (<juce_core/juce_core.h>), etc.

  2. I believe you’d get better results by adding this to the CMakeLists:

set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
  1. Unfortunately due to the bug above you’d get much better results using an IDE like Visual Studio (not code), CLion or Xcode. The bug still exists in those IDEs, but much less so and you get dramatically less false positives and better auto complete.
1 Like

Hi Eyal, thanks for the quick response!

I’ll give it a vote - it seems to be a common issue!

I messed about with some of my intellisense settings and found that clangd specifically was causing issues. The VSCode default seems to handle it fine.

set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")

I’ll add that to my CMakeLists. What exactly does it do?

Re: IDE, I prefer VSCode since it’s much more lightweight, and am trying to avoid directly working in Visual Studio (which does handle it all fine). CLion would be ideal to be honest, but I can’t afford it at the moment.

P.s. I found your resources on CMake on TAP very useful - keep up the good work!

The Compile_Commands thing outputs a JSON file that gives VS Code additional information about the compile commands that are called. That usually tells the editor about include directories and other bits that helps it parse the context of the code.

https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html

1 Like

I am joining this thread - has anyone ever fixed IntelliSense in vscode at all? I mean while using Juce and JuceHeader.h?
I made sure the include path to the generated JuceHeader.h is added to the c_cpp_properties.json file, then I checked that my compile_commands.json does indeed add the correct path to JuceHeader for each and every compile commands in the file. Still, it’s as if it had no effect at all. I just keep seeing the errors in the IDE about not finding JuceHeader, it basically makes the IDE error listing useless. I have spent multiple nights on this issue in the past year, and I always gave up without results.
Can this be fixed at all? Or the number of developers using vscode is so minimal that it is not worth it?

Sorry for the slow reply!

I did eventually manage to get something working, I think by building the project on the command line first, so that the JuceHeader.h can be generated, but I really was throwing stuff at a wall and eventually something stuck. I’ll see if I can recreate it and try and get back to you.

Also afterwards, CLion came out with a free license for hobby & open-source projects, so I made the switch and have never looked back / encountered the same issue :stuck_out_tongue: