--I want to create a Cpp project for Linux on a Windows PC --I searched for CMake's Docker container but couldn't find it, so I created it this time.
--Set to mount local folder --Editing code on a local folder will immediately reflect the changes in the container --Can be edited with an editor such as VS code without logging in to the container --You can also retrieve the built files without using the docker cp command.
-① Put .cpp files and CMakeLists.txt in the cpp folder. --Edit with your favorite editor such as VSCode -② Enter the container and execute the build --③ The built file can be retrieved from the cpp folder in the local environment.
--Place Dockerfile and docker-compose.yml as shown below. --Source code is listed at the bottom of the article --The cpp folder is automatically generated by docker-compose up --Put the source code in this folder
tree
Folder structure
├── Dockerfile
├── docker-compose.yml
└── cpp ← Automatically generated
Dockerfile
Dockerfile
FROM ubuntu:18.04
RUN apt-get update && \
apt-get install -y sudo && \
apt-get install build-essential -y && \
apt-get install -y vim && \
apt-get install -y wget && \
apt-get install -y unzip
# Install compilers.
RUN apt-get install -y gcc && \
apt-get install -y g++
RUN apt-get install -y cmake 3.10.2
# SET path to compilers.
# https://stackoverflow.com/questions/17275348/how-to-specify-new-gcc-path-for-cmake
ENV CC=/usr/bin/gcc \
CXX=/usr/bin/g++
# OpenBlas, Lapack
RUN apt-get install -y libopenblas-dev && \
apt-get install -y liblapack-dev
# Please use below directory to install cpp libraries.
WORKDIR $HOME/usr/
RUN mkdir ./library
CMD bash
docker-compose.yml
docker-compose.yml
version: '3'
services:
cmake:
container_name: cmake_container
build:
context: .
dockerfile: Dockerfile
tty: true
command: /bin/bash
volumes:
- ./cpp:/usr/cpp
Local environment
#First launch of container
docker-compose up -d
#Enter the running container
docker-compose run cmake
@make_container
#Enter the cpp folder inside the container
cd cpp
#Create and enter the build folder
mkdir build
cd build
#run cmake
cmake ../
#Run make
make
--Do not extract or install OS-dependent libraries in the cpp folder.
--I got the following error message and the container was unmounted.
- docker compose error while creating mount source path...
--Create another folder in the container for dependent libraries and install it there.
--Example) OpenCV
――In the following, we will introduce how to build the source code that says Hello CMake!
.
--Create the following two files in the cpp folder.
Configuration in the cpp folder
cpp
├── main.cpp
└── CMakeLists.txt
--Copy the following contents to each file.
main.cpp
#include <iostream>
#include <string>
int main(void){
std::cout << "Hello CMake!" << std::endl;
return 0;
}
CMakeLists.txt
#Project name
project("testCMake")
#Set CMake version
cmake_minimum_required(VERSION 2.8)
# testCMake.The executable file called out is main.Created from cpp
add_executable(testCMake main.cpp)
――Next, we will start the build work in the container. --Enter the following commands in order to build.
@cmake_container
#Enter the cpp folder inside the container
cd cpp
#Create a build folder
mkdir build
cd build
#Build execution
cmake ../
make
--Let's run the last built program!
--If you enter ./testCMake
and output Hello CMake !, you are successful.
@cmake_container
./testCMake
Hello CMake!
--The Docker image introduced in this article can be downloaded from DockerHub.
--Please refer to this article for how to use CMake.
Recommended Posts