Skycoder42 / QHotkey

A global shortcut/hotkey for Desktop Qt-Applications
BSD 3-Clause "New" or "Revised" License
552 stars 162 forks source link

CMake not working with FetchContent #24

Closed pfriesch closed 5 years ago

pfriesch commented 5 years ago

CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)
project(qhotkey_test)

set(CMAKE_CXX_STANDARD 14)

include(FetchContent)
FetchContent_Declare(
        qhotkey
        GIT_REPOSITORY https://github.com/Skycoder42/QHotkey.git
        GIT_TAG        master
)

add_executable(qhotkey_test main.cpp)

FetchContent_MakeAvailable(qhotkey)

main.cpp:

#include <iostream>

#include <QHotkey>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

When making this test project, I get fatal error: 'QHotkey' file not found. Maybe @mujx knows more?

Skycoder42 commented 5 years ago

I do not know about FetchContent. If it does not work with the repository, that thats a problem of that tool, not the repository.

However, the reason is most likely that sources are in the QHotkey subfolder. You propably have to pass some extra arguments to tell FetchContent where to find the sources

Skycoder42 commented 5 years ago

Checking the Documentation of FetchContent, you propably have to do it in multiple steps and specify a custom path.

mujx commented 5 years ago

@pfriesch I managed to build the example using FetchContent. In your example you didn't link the library and include its headers in your executable. FetchContent just fetches the dependency and makes it available for further configuration.

Also the library tests were causing some issues while building which I didn't investigate further (maybe we can disable them by default).

cmake_minimum_required(VERSION 3.14)
project(qhotkey_test)

set(CMAKE_CXX_STANDARD 14)

include(FetchContent)
FetchContent_Declare(
  qhotkey
  GIT_REPOSITORY https://github.com/Skycoder42/QHotkey.git
  GIT_TAG        master
)

set(QHOTKEY_EXAMPLES OFF CACHE INTERNAL "")

FetchContent_GetProperties(qhotkey)
if(NOT qhotkey_POPULATED)   
  FetchContent_Populate(qhotkey)
  message(STATUS "QHotkey source dir: ${qhotkey_SOURCE_DIR}")
  message(STATUS "QHotkey binary dir: ${qhotkey_BINARY_DIR}")
  add_subdirectory(${qhotkey_SOURCE_DIR} ${qhotkey_BINARY_DIR}) 
endif()

add_executable(qhotkey_test main.cpp)
target_link_libraries(qhotkey_test QHotkey::QHotkey)
target_include_directories(qhotkey_test PUBLIC ${qhotkey_SOURCE_DIR}/QHotkey)

Output when QHOTKEY_EXAMPLES is not set to OFF.

root@587b41291271:~/qhotkey# cmake --build build/                                            
-- QHotkey source dir: /root/qhotkey/build/_deps/qhotkey-src                                 
-- QHotkey binary dir: /root/qhotkey/build/_deps/qhotkey-build                                                                                                                            
-- Configuring done                                                                          
-- Generating done                                                                           
-- Build files have been written to: /root/qhotkey/build                                     
[ 46%] Built target qhotkey                                                                  
Scanning dependencies of target qhotkey_test                                                 
[ 53%] Building CXX object CMakeFiles/qhotkey_test.dir/main.cpp.o                                                                                                                         
[ 60%] Linking CXX executable qhotkey_test                                                                                                                                                
[ 60%] Built target qhotkey_test                                                             
[ 66%] Generating moc_hottestwidget.cpp                                                      
[ 73%] Generating ui_hottestwidget.h                                                                                                                                                      
Scanning dependencies of target HotkeyTest                                                   
[ 80%] Building CXX object _deps/qhotkey-build/HotkeyTest/CMakeFiles/HotkeyTest.dir/main.cpp.o                                                                                            
In file included from /root/qhotkey/build/_deps/qhotkey-src/HotkeyTest/main.cpp:1:                                                                                                        
/root/qhotkey/build/_deps/qhotkey-src/HotkeyTest/hottestwidget.h:5:10: fatal error: QHotkey: No such file or directory                                                                    
 #include <QHotkey>                                                                          
          ^~~~~~~~~                                                                          
compilation terminated.          
pfriesch commented 5 years ago

Thank you @mujx, cmake is still quite obscure for me! Yeah, that makes much more sense now. This is working for me now:

cmake_minimum_required(VERSION 3.14)
project(qhotkey_test)

set(CMAKE_CXX_STANDARD 14)

include(FetchContent)
FetchContent_Declare(
        qhotkey
        GIT_REPOSITORY https://github.com/Skycoder42/QHotkey.git
        GIT_TAG        master
)
FetchContent_MakeAvailable(qhotkey)

add_executable(qhotkey_test main.cpp)

target_link_libraries(qhotkey_test QHotkey::QHotkey)
target_include_directories(qhotkey_test PUBLIC ${qhotkey_SOURCE_DIR}/QHotkey)