How do I make a C++ 3rd party library feched with CMake's FetchContent not show any warnings?

352 Views Asked by At

I'm working on a C++ project that uses wxWidgets with a CMake script I wrote to resolve this dependency when I build it. To accomplish that I used CMake's module FetchContent and it currently works properly, resolving the dependency, compiling and running.

However, it keeps showing me warnings relative to the wxWidgets files and I do not desire that, since it mixes with my code's warnings, making it hard for me to track them down and resolve them (VS Code showed over a thousand warnings in my project).

Right now my CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.11 FATAL_ERROR)
project(my_project LANGUAGES CXX)

# Using C++20
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Use wxWidgets static libraries
set(wxWidgets_USE_STATIC 1)
set(wxBUILD_SHARED OFF)

# Include FetchContent module
include(FetchContent)

# Fetch wxWidgets dependency statement declaration
FetchContent_Declare(
    wxWidgets
    GIT_REPOSITORY
        https://github.com/wxWidgets/wxWidgets.git
    GIT_TAG
        v3.1.4
)

# Message to inform user that script is not bugged, just busy resolving dependecy
message(STATUS "Baixando wxWidgets e incluindo-o no projeto")

# Effectively fetch wxWidgets dependency
FetchContent_GetProperties(wxWidgets)

if(NOT wxWidgets_POPULATED)
    FetchContent_Populate(wxWidgets)
    add_subdirectory(${wxwidgets_SOURCE_DIR} ${wxwidgets_BUILD_DIR})
endif()

# Include all of the program's header and source files
file(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_LIST_DIR}/include/*.h)
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_LIST_DIR}/source/*.cpp)

# Create program executable and link against all of the program's header and source files
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})

# Link wxWidgets libraries against program executable
target_include_directories(${PROJECT_NAME} PRIVATE ${wxwidgets_SOURCE_DIR}/include)
target_link_libraries(${PROJECT_NAME} PRIVATE wx::base wx::core)

# Copy all assets to build directory to make them available to the program
add_custom_target(copy_assets
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/assets ${CMAKE_CURRENT_BINARY_DIR}/assets
)
add_dependencies(${PROJECT_NAME} copy_assets)

I am looking for a way to make this script apply warnings only to my code, but looking for it on the internet I haven't found anything particularly helpful (saw some similar stuff, but nothing that I could adapt to my specific case). Am I doing this whole FetchContent thing wrong? It is my first time trying anything of the kind so it is very possible. In that case, how should I do it? My intention is to link those libraries statically to my program.

1

There are 1 best solutions below

0
On

For anyone who finds this question when facing a similar problem, I'll leave my conclusion here.

I've tried JeJo's solution but I couldn't make it work since I didn't know the wxWidgets actual library name, all i could find were its aliases, which do not work on set_target_properties.

However, having added my compile options to the program (thanks JeJo), I've noticed something: When I first configure my project (fetching wx, compiling it, essentially all the pre build stuff) it shows all those warnings from the wxWidgets compilation stage. However, as soon as I actually build my project they all just vanish. I'll take that as a win I guess.

Since this project of mine was supposed to be a template for wxWidgets based projects (btw check it out on Github https://github.com/lucas-yotsui/My-wxWidgets-Template) I'll just add a note to the README talking about that and hope that users read it carefully.

Thanks to everyone who took some time to help me and please if you can, check out my project on Github and leave some feedback, it is actually my first serious public project, since I'm trying to start programming for real now. I'd really appreciate any suggestions there and I'm hoping it will help someone someday. (BTW I'm brazilian, so the documentation is mostly written in portuguese, but it should be rather simple to translate using Google translate or something).