一、同级文件夹下代码调用问题
目录如下:
./testCMake(根目录):
/build:
/MyClass:
CMakeLists.txt
MyClass.cpp
MyClass.h
/MyFunction:
CMakeLists.txt
MyFunction.cpp
MyFunction.h
CMakeLists.txt
main.cpp
上述文件调用关系为:main.cpp调用MyClass.h和MyFunction.h;同级目录下的MyFunction.h调用另外一个同级目录下MyClass.h。上述文件内容具体如下:
./MyClass/CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
set(CMAKE_CXX_STANDARD 14)
#搜集MyClass目录下的文件
aux_source_directory(. MYCLASS_SOURCE)
#生成链接库
add_library(MyClass ${MYCLASS_SOURCE})
./MyClass/MyClass.cpp
#include "MyClass.h"
void MyClass::test(std::string s){
std::cout<<"----"<<s<<std::endl;
}
./MyClass/MyClass.h
#pragma once
#include <iostream>
#include <string>
class MyClass{
public:
void test(std::string);
};
./MyFunction/CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
set(CMAKE_CXX_STANDARD 14)
#生成链接库
add_library(MyFunction MyFunction.h MyFunction.cpp)
#目标链接库MyFunction添加链接库MyClass
target_link_libraries(MyFunction MyClass)
#将目录MyClass添加的目标目录中
target_include_directories(MyFunction PUBLIC ..)
./MyFunction/MyFunction.cpp
#include "MyFunction.h"
void MyFunction(){
MyClass myClass;
myClass.test("MyFunction");
}
./MyFunction/MyFunction.h
#pragma once
#include "MyClass/MyClass.h"
void MyFunction();
./CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(HelloWorld)
set(CMAKE_CXX_STANDARD 14)
#添加子目录 MyClass/ 并且也会处理 MyClass目录下的 CMakeLists.txt文件
add_subdirectory(MyClass)
add_subdirectory(MyFunction)
#生成可执行文件 HelloWorld
add_executable(HelloWorld main.cpp)
#将可执行文件添加链接库MyFunction
target_link_libraries(HelloWorld MyFunction)
#将目录MyFunction添加到HelloWorld目标路径中
target_include_directories(HelloWorld PUBLIC MyFunction)
target_include_directories(HelloWorld PUBLIC MyClass)
./main.cpp
#include <iostream>
// 下面与./CMakeLists.txt中的target_include_directories(HelloWorld PUBLIC MyFunction)
// 和target_include_directories(HelloWorld PUBLIC MyClass)配套
// 如果不加这两句,下列两句应该写成 #include"MyClass/MyClass.h"和#include"MyFunction/MyFunction.h"
#include"MyClass.h"
#include"MyFunction.h"
int main()
{
MyClass myClass;
myClass.test("main");
MyFunction();
return 0;
}
二、CMakeLists.txt第三方库添加问题
该项目添加的第三方库有boost,opencv,paddleOCR,paddleX,websocketpp,海康工业相机SDK,海康监控摄像头SDK。
工程目录结构图如下:
./CMakeLists.txt
cmake_minimum_required (VERSION 3.12)
project (JMC_AI_VISION)
set(CMAKE_POSITION_INDEPENDENT_CODE ON) # 生成与路径无关代码
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-std=c++11 -fpermissive -O2 ${SSE_FLAGS}")
SET(DEPS "" CACHE PATH "Location of libraries")
set(EXTERNAL_LIB "-ldl -lrt -lgomp -lz -lm -lpthread")
set(DEPS ${DEPS} ${EXTERNAL_LIB})
# boost
add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY)
#find_package(Boost)
#message( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}")
#message( STATUS " Boost_LIBRARIES = ${Boost_LIBRARIES}")
#message( STATUS " Boost_LIB_VERSION = ${Boost_LIB_VERSION}")
#set(DEPS ${DEPS} ${Boost_LIBRARIES})
#include_directories(${Boost_INCLUDE_DIRS})
#OPENCV
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
set(DEPS ${DEPS} ${OpenCV_LIBS})
include_directories(${OpenCV_INCLUDE_DIRS})
#paddleOCR
include_directories("/paddle/paddle_inference/third_party/install/protobuf/include")
include_directories("/paddle/paddle_inference/third_party/install/glog/include")
include_directories("/paddle/paddle_inference/third_party/install/gflags/include")
include_directories("/paddle/paddle_inference/third_party/install/xxhash/include")
include_directories("/paddle/paddle_inference/paddle/include")
include_directories("/paddle/PaddleOCR-release-2.6/deploy/cpp_infer/build/third-party/extern_autolog-src")
link_directories("/paddle/paddle_inference/third_party/install/protobuf/lib")
link_directories("/paddle/paddle_inference/third_party/install/glog/lib")
link_directories("/paddle/paddle_inference/third_party/install/gflags/lib")
link_directories("/paddle/paddle_inference/third_party/install/xxhash/lib")
link_directories("/paddle/paddle_inference/paddle/lib")
#set(DEPS ${DEPS} /paddle/paddle_inference/paddle/lib/libpaddle_inference${CMAKE_SHARED_LIBRARY_SUFFIX})
set(DEPS ${DEPS} glog gflags protobuf xxhash cryptopp)
#paddleX
include_directories("/paddle/PaddleX/deploy/cpp/encryption/include")
include_directories("/paddle/PaddleX/deploy/cpp/model_deploy/common/include")
include_directories("/paddle/PaddleX/deploy/cpp/model_deploy/engine/include")
include_directories("/paddle/PaddleX/deploy/cpp/model_deploy/paddlex/include")
include_directories("/paddle/PaddleX/deploy/cpp/model_deploy/ppclas/include")
include_directories("/paddle/PaddleX/deploy/cpp/model_deploy/ppdet/include")
include_directories("/paddle/PaddleX/deploy/cpp/model_deploy/ppseg/include")
include_directories("/paddle/PaddleX/deploy/cpp/model_deploy/utils/include")
include_directories("/paddle/PaddleX/deploy/cpp")
include_directories("/paddle/PaddleX/deploy/cpp/build/ext/yaml-cpp/src/ext-yaml-cpp/include")
link_directories("/paddle/PaddleX/deploy/cpp/build/ext/yaml-cpp/lib")
link_directories("/paddle/paddle_inference/third_party/install/cryptopp/lib")
link_directories("/paddle/PaddleX/deploy/cpp/build/lib")
set(DEPS ${DEPS} yaml-cpp cryptopp model_infer)
#websocket
find_package(websocketpp REQUIRED)
include_directories(${websocketpp_INCLUDE_DIRS})
#include_directories("/paddle/websocketpp-master")
#link_directories("/paddle/websocketpp-master")
set(DEPS ${DEPS} ${websocketpp_LIBRARIES})
#message("-----websocketpp_LIBRARIES = ${websocketpp_LIBRARIES}")
#hk_i_camera and hk_s_camera
find_library(MVS_LIB_I MvCameraControl HINTS ${PROJECT_SOURCE_DIR}/third_party/hk_cam_i/lib REQUIRED)
include_directories(${PROJECT_SOURCE_DIR}/third_party/hk_cam_i/ind)
set(DEPS ${DEPS} ${MVS_LIB_I})
#include_directories("/opt/MVS/include")
#link_directories("/opt/MVS/lib/64/")
find_library(MVS_LIB_S hcnetsdk HINTS ${PROJECT_SOURCE_DIR}/third_party/hk_cam_s/lib ${PROJECT_SOURCE_DIR}/third_party/hk_cam_s/lib/HCNetSDKCom REQUIRED)
find_library(ctrllib PlayCtrl HINTS ${PROJECT_SOURCE_DIR}/third_party/hk_cam_s/lib)
include_directories(${PROJECT_SOURCE_DIR}/third_party/hk_cam_s/ind)
set(DEPS ${DEPS} ${MVS_LIB_S})
set(DEPS ${DEPS} ${ctrllib})
message("-----DEPS = ${DEPS}")
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
add_subdirectory(zyyai)
add_subdirectory(zyyocr)
# add_subdirectory(jmc)
include_directories(${PROJECT_SOURCE_DIR})
add_executable(spare_tire_running spare_tire_main.cpp spare_tire.cpp JMC_common.cpp JMC_device.cpp web_socketpp.cpp)
# ADD_DEPENDENCIES(spare_tire_running ext-yaml-cpp)
target_link_libraries(spare_tire_running ${DEPS} zyyAi zyyOCR)
./zyyai/CMakeLists.txt
include_directories(
${PROJECT_SOURCE_DIR}/zyyai
)
aux_source_directory(. LIB_SRCS) # 添加当前目录下所有的源文件
add_library (zyyAi ${LIB_SRCS}) # 当前目录下的文件生成一个链接库
target_link_libraries(zyyAi ${DEPS})
#set(DEPS ${DEPS} ${zyyAi})
./zyyocr/CMakeLists.txt
include_directories(
${PROJECT_SOURCE_DIR}/zyyocr
)
aux_source_directory(. LIB_SRCS) # 添加当前目录下所有的源文件
add_library (zyyOCR ${LIB_SRCS}) # 当前目录下的文件生成一个链接库
target_link_libraries(zyyOCR ${DEPS})
#set(DEPS ${DEPS} ${zyyOCR})