首页 文章

在Docker / CMake项目中包含外部库

提问于
浏览
0

我正在使用docker和cmake构建一个cxx项目,我现在的任务是集成我在本地的第三方库 .

为了开始,我添加了一个项目,其中只包含一个src文件夹和一个带有main函数的cpp文件,以及我需要从上面提到的库中包含的内容 . 此时,我已经陷入困境,因为当我在docker环境中构建时,找不到包含的文件 . 当我在项目中调用没有docker的cmake时,我没有得到包含错误 .

我的目录树:

my_new_project
    CMakeLists.txt
    src
        my_new_project.cpp

CMakeLists.txt 我有以下内容:

CMAKE_MINIMUM_REQUIRED (VERSION 3.6)

project(my_new_project CXX)
file(GLOB SRC_FILES src/*.cpp)
add_executable(${PROJECT_NAME} ${SRC_FILES})

include_directories(/home/me/third_party_lib/include)

在Docker环境中进行构建需要什么?我是否需要将第三方库转换为另一个项目并将其添加为依赖项(类似于我对GitHub项目的处理方式)?

我会很高兴任何指向正确的方向!

编辑:

我已经复制了整个第三方项目的根目录,现在可以使用 include_directories(/work/third_party_lib/include) 添加包含目录,但这是不是可行的方法?

1 回答

  • 0

    在构建新的dockerized应用程序时,需要 COPY/ADD 所有src,build和cmake文件,并在 Dockerfile 中定义 RUN 指令 . 这将用于构建您的docker image ,它捕获所有必需的二进制文件,资源,依赖项等 . 一旦构建了映像,您就可以在docker上运行该映像的容器,它可以暴露端口,绑定卷,设备,等你的申请 .

    基本上,创建你的 Dockerfile

    # Get the GCC preinstalled image from Docker Hub
    FROM gcc:4.9
    
    # Copy the source files under /usr/src
    COPY ./src/my_new_project /usr/src/my_new_project
    
    # Copy any other extra libraries or dependencies from your machine into the image
    COPY /home/me/third_party_lib/include /src/third_party_lib/include
    
    # Specify the working directory in the image
    WORKDIR /usr/src/
    
    # Run your cmake instruction you would run
    RUN cmake -DKRISLIBRARY_INCLUDE_DIR=/usr/src/third_party_lib/include -DKRISLIBRARY_LIBRARY=/usr/src/third_party_lib/include ./ && \
    make && \
    make install
    
    # OR Use GCC to compile the my_new_project source file
    # RUN g++ -o my_new_project my_new_project.cpp
    
    # Run the program output from the previous step
    CMD ["./my_new_project"]
    

    然后,您可以执行 docker build . -t my_new_project 然后 docker run my_new_project 来尝试它 .

    还有几个很好的例子来构建C **应用程序作为docker容器:

    有关此问题的更多信息,请参阅docker文档:

相关问题