0

I have the following structure of my project:

Project
-- CMakeLists.txt
-- src/
-- -- main.cpp
-- include/
-- -- curl/
-- -- -- *.h files
-- -- nlohmann/
-- -- -- *.hpp files
-- lib/
-- -- libcurl.lib

Project/CMakeLists:

cmake_minimum_required(VERSION 3.1)
set(PROJECT_NAME "VKAPI")
project(${PROJECT_NAME} CXX)

include_directories(${PROJECT_SOURCE_DIR}/include)

file(GLOB TARGET_SRC "./src/*.cpp")

add_executable(main ${TARGET_SRC})
target_link_libraries(main "${CMAKE_SOURCE_DIR}/lib/libcurl.lib")

install(TARGETS main DESTINATION bin)

It works fine, but when I launch main.exe, it doesn't work. Instead of executing correctly and displaying the message, it ends with the code 0. Code:

#include <iostream>

#include "curl/curl.h"
#include "nlohmann/json.hpp"

int main()
{
    CURL* curl = curl_easy_init();

    std::cout << "That's work!" << std::endl;
    std::cout << "Hello Easy C++ project!" << std::endl;
}

But when I delete "#include "curl/curl.h"" and "#include "nlohmann/json.hpp"", it started to work fine.

qucals
  • 27
  • 7
  • Maybe this thread help [link](https://stackoverflow.com/questions/16655705/cmake-cannot-determine-linker-language-for-target) – arsham belivani Jul 26 '20 at 07:31
  • `TARGET_CURL "./*.h")` curl target has only headers. Headers don't mean much. So is this curl target a C++ or C project? Note that you are not linking _libraries_ but _cmake targets_. – KamilCuk Jul 26 '20 at 07:39
  • @KamilCuk, it's a C++ project. – qucals Jul 26 '20 at 07:50
  • https://stackoverflow.com/questions/11801186/cmake-unable-to-determine-linker-language-with-c second asnwer – KamilCuk Jul 26 '20 at 07:51
  • @KamilCuk, it did'nt help me. – qucals Jul 26 '20 at 08:02
  • No? Doing `set_target_properties(curl PROPERTIES LINKER_LANGUAGE CXX)` and similar for nlo-something should have worked. – KamilCuk Jul 26 '20 at 08:03
  • @KamilCuk, it didn't help me too. I decided to rewrite CMakeLists, check updates. – qucals Jul 26 '20 at 08:40
  • Please see the responses [here](https://stackoverflow.com/questions/8774593/cmake-link-to-external-library) for how to properly link external libraries using CMake. – Kevin Jul 26 '20 at 12:16

2 Answers2

0

This:

file(GLOB TARGET_HEADERS "*.hpp")

add_library(nlohmann ${TARGET_HEADERS})

Is wrong in two ways:

  1. You should not glob your sources, you should list them explicitly. There is plenty of guidance on this written already, globbing is a no-no in CMake.
  2. Your "library" does not contain any source code. A library in CMake requires source code such as a .c or .cpp file to compile. You actually don't need to mention the header files at all, just tell CMake about the files it needs to compile, not the headers.
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

My error in the line: target_link_libraries(main "${CMAKE_SOURCE_DIR}/lib/libcurl.lib") It worked when I added a word INTERFACE: target_link_libraries(main INTERFACE "${CMAKE_SOURCE_DIR}/lib/libcurl.lib")

qucals
  • 27
  • 7