C++工程,cmake,cmake生成so包并调用

1,目录结构

在这里插入图片描述

2,代码清单

2.1,utils.h

#ifndef __UTILS_H__
#define __UTILS_H__

#include <iostream>

class Utils {
    private:

    public:
        static void print(std::string str);
};

void print(std::string str);

#endif

2.2,utils.cpp

#include "utils.h"

void Utils::print(std::string str) {
    std::cout << "utils print:" << str << std::endl;
}

void print(std::string str) {
    std::cout << "print:" << str << std::endl;
}

2.3,main.cpp

#include <iostream>
#include "utils.h"

int main() {
    std::string str = "hello";
    Utils::print(str);
    print(str);
    std::cout << "hellow world!" << std::endl;
    return 0;
}

2.4,顶层CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

if(CMAKE_COMPILER_IS_GNUCC)
    message("COMPILER IS GNUCC")
        ADD_DEFINITIONS ( -std=c++11 )
endif(CMAKE_COMPILER_IS_GNUCC)

SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -ggdb3")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/module)
ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/process)

2.5,so包(module)CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(module)

if(CMAKE_COMPILER_IS_GNUCC)
    message("COMPILER IS GNUCC")
        ADD_DEFINITIONS ( -std=c++11 )
endif(CMAKE_COMPILER_IS_GNUCC)

SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -ggdb3")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
# 3. opencv
#link_directories("/usr/local/lib")
#include_directories("/usr/local/include/opencv4")

# 5. 添加头文件路径
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/utils)
# 6. 添加源文件
FILE(GLOB_RECURSE SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
message(${CMAKE_CURRENT_SOURCE_DIR})
message(${SOURCE_FILES})
# 7. 添加链接库
#LINK_LIBRARIES(opencv_core opencv_highgui)

# 9. 设置环境变量,编译用到的源文件全部都要放到这里,否则编译能够通过,
#但是执行的时候会出现各种问题,比如"symbol lookup error xxxxx , undefined symbol"
SET(ALL_SRCS ${SOURCE_FILES})
message(${ALL_SRCS})

# 生成so包
ADD_LIBRARY(${PROJECT_NAME} SHARED ${ALL_SRCS})

# 10.add executable file,添加要编译的可执行文件
#ADD_EXECUTABLE(${PROJECT_NAME} ${ALL_SRCS})

2.6,工程(process)CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(hello)

if(CMAKE_COMPILER_IS_GNUCC)
    message("COMPILER IS GNUCC")
        ADD_DEFINITIONS ( -std=c++11 )
endif(CMAKE_COMPILER_IS_GNUCC)

#SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -ggdb3")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

# 5. 添加头文件路径
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/module/include/utils)
# 6. 添加源文件
FILE(GLOB_RECURSE SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
message(${CMAKE_CURRENT_SOURCE_DIR})
message(${SOURCE_FILES})
# 7. 添加链接库

LINK_DIRECTORIES(${CMAKE_BINARY_DIR}/module)
LINK_LIBRARIES(module)


# 9. 设置环境变量,编译用到的源文件全部都要放到这里,否则编译能够通过,
#但是执行的时候会出现各种问题,比如"symbol lookup error xxxxx , undefined symbol"
SET(ALL_SRCS ${SOURCE_FILES})
message(${ALL_SRCS})

# 10.add executable file,添加要编译的可执行文件
ADD_EXECUTABLE(${PROJECT_NAME} ${ALL_SRCS})

# 不可以
#TARGET_LINK_LIBRARIES(${CMAKE_BINARY_DIR}/module/libmodule.so)

3,编译运行

在这里插入图片描述

在这里插入图片描述

转自:https://blog.csdn.net/m0_37542524/article/details/104867734

posted @ 2022-10-14 10:20  MasonLee  阅读(859)  评论(0编辑  收藏  举报