Quickly build a Linux C/C++ development environment on macOS in ten minutes

There is a C project using Epoll. The main Linux development machine I usually use is not with me. I want to develop and debug on macOS, but there is no Linux virtual machine. It just so happens that JetBrains CLion's Toolchains configuration supports SSH and Docker in addition to using the local environment.

Using CLion + Docker Desktop, the author can build a development environment on macOS that can develop C projects using Linux class libraries in less than ten minutes, and has a good development experience.

Prepare the Docker environment

For macOS, you can use Docker Desktop, which will not be described in this article.
Of course, the Docker environment may not be local, and you can connect to the non-local Docker environment through TCP or SSH.

insert image description here

Build an Image that contains the dependencies required by the project

Prepare Dockerfile and install required dependencies

The author's main development machine is Ubuntu 22.04, and I choose the same system as the base image.
Mirroring can be built according to the dependencies required by the project. The following is the Dockerfile for building a basic C/C++ environment.

FROM ubuntu:22.04
MAINTAINER [email protected]
RUN apt update && apt install -y cmake g++ gdb

Tip: If the environment cannot speed up access to the official Ubuntu apt source, it is recommended to use the domestic apt source.

Overwrite the original sources.list in the base image with sources.list.

FROM ubuntu:22.04
MAINTAINER [email protected]
ARG TARGETARCH
COPY $TARGETARCH/sources.list /etc/apt/sources.list
RUN apt update && apt install -y cmake g++ gdb

The environment for building x86/64 architecture can use the following sources.list

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

The environment for building aarch64 and other architectures can use the following sources.list

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse

build image

>>> docker build -t teslacn/cmake:1.0 .
[+] Building 16.4s (8/8) FINISHED                                            
 => [internal] load build definition from Dockerfile                    0.0s
 => => transferring dockerfile: 37B                                     0.0s
 => [internal] load .dockerignore                                       0.0s
 => => transferring context: 2B                                         0.0s
 => [internal] load metadata for docker.io/library/ubuntu:22.04        16.3s
 => [1/3] FROM docker.io/library/ubuntu:22.04@sha256:f154feaf13b51d16e  0.0s
 => [internal] load build context                                       0.0s
 => => transferring context: 65B                                        0.0s
 => CACHED [2/3] COPY arm64/sources.list /etc/apt/sources.list          0.0s
 => CACHED [3/3] RUN apt update && apt install -y cmake g++ gdb         0.0s
 => exporting to image                                                  0.0s
 => => exporting layers                                                 0.0s
 => => writing image sha256:b4ad8d6c3284e79dedcf8c628b1a89c0bb59d29000  0.0s
 => => naming to docker.io/teslacn/cmake:1.0                            0.0s

CLion Configure Toolchains

Toolchains add Docker environment

Just select the built image, and CLion will automatically detect the version of the build tool, compiler, and GDB.
insert image description here

The CMake project selects the Docker Toolchain just added

insert image description here

Development experience

header file navigation

CLion automatically caches the Linux header files in the container locally, and can directly navigate to sys/epoll.hthe files from the code, which is no different from developing directly in the Linux environment.
insert image description here

run directly

There is no difference at all compared to running locally normally.
insert image description here

Commissioning

Debugging and running found that, except that the program log is not output to stdout, other debugging functions are completely the same as local debugging.

insert image description here

insert image description here

The above is the process and result of environment construction.

[Update] CLion Debug console has no logs

Solution:

setbuf(stdout, 0);

https://stackoverflow.com/questions/47776094/clion-wont-show-output-in-debug

Guess you like

Origin blog.csdn.net/wu_weijie/article/details/130142115