c++ – Problem occured in writting CMakeLists about include_directories and add_subdirectory on add_library
I am new to CMake project and
I am trying to write a simple test with libmodbus and paho-mqtt
on VSCode with GCC 8.1.0 x86_64-w64-mingw32.
In my main.cpp both head file can be included normally,
#include "modbus.h"
#include "MQTTClient.h"
int main(int argc, char *argv[]){
return 0;
}
and CMakeLists.txt like this
cmake_minimum_required(VERSION 3.5.0)
project(Project_Testbed VERSION 0.1.0 LANGUAGES C CXX)
add_executable(${PROJECT_NAME} main.cpp)
include_directories(ext/libmodbus)
target_link_directories(${PROJECT_NAME} PUBLIC ext/libmodbus)
target_link_libraries(${PROJECT_NAME} PUBLIC modbus)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/ext/libmodbus/modbus.dll
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
add_subdirectory(model/model_mqtt)
include_directories(model/model_mqtt)
target_link_libraries(${PROJECT_NAME} PUBLIC model_mqtt)
include_directories(ext/mqtt)
target_link_directories(${PROJECT_NAME} PUBLIC ext/mqtt)
target_link_libraries(${PROJECT_NAME} PUBLIC paho-mqtt3a)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/ext/mqtt/paho-mqtt3a.dll
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
But when I try to do it in subproject(model_mqtt.cpp), like this
#include "modbus.h"
#include "MQTTClient.h"
int test(int argc, char *argv[]){
return 0;
}
and CMakeLists.txt like this
project(model_mqtt)
set(mqtt_files
model_mqtt.cpp
model_mqtt.h
)
add_library(model_mqtt ${mqtt_files})
target_link_libraries(${PROJECT_NAME} PUBLIC modbus)
target_link_libraries(${PROJECT_NAME} PUBLIC paho-mqtt3a)
It shows: cannot open source file “MQTTClient.h”C/C++(1696)
How could it be? Isn’t them the same?
My project structure is like this
project
|──ext
| |──mqtt
| | MQTTClient.h
| | paho-mqtt3a.lib
| | paho-mqtt3a.dll
| | etc...
| |──libmodbus
| | modbus.h
| | modbus.lib
| | modbus.dll
| | etc...
|──model
| |──model_mqtt
| | CMakeLists.txt
| | model_mqtt.cpp
| | model_mqtt.h
|──CMakeLists.txt
|──main.cpp
|──main.h
Read more here: Source link