Mac的C/C++的默认编译器为Clang,与Linux的gcc和g++编译器有很多地方是存在问题的。所以在Mac上搭建一个Linux系统的开发环境对于在Mac上开发软件非常有必要。本文介绍的是在Mac上,结合Docker,以Clion作为开发工具的方法。
安装Docker
在Mac上安装Docker非常简单,可以在以下地址下载安装。按照正常流程安装就行,不再详细介绍。
www.docker.com/products/docker-desktop
在Docker中安装Ubuntu系统和开发编译器
利用Dockerfile,从Docker Hub拉取Ubuntu镜像,让后对镜像设置,新建如下4个文件:
- 新建Dockerfile,内容如下
FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y build-essential
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y cmake
RUN apt-get install -y gdb
RUN apt-get install -y net-tools
RUN apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN sed -ri 's/^PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config
RUN apt-get install -y rsync
RUN sed -ri 's/RSYNC_ENABLE=false/RSYNC_ENABLE=true/g' /etc/default/rsync
COPY rsync.conf /etc
RUN echo 'root:000000' |chpasswd
RUN mkdir /root/sync
COPY entrypoint.sh /sbin
RUN chmod +x /sbin/entrypoint.sh
ENTRYPOINT [ "/sbin/entrypoint.sh" ]
- 新建entrypoint.sh,内容如下
#!/bin/bash
/usr/bin/rsync --daemon --config=/etc/rsync.conf
/usr/sbin/sshd -D
- 新建rsync.conf,内容如下
# 编辑配置信息
max connections = 8
log file = /var/log/rsync.log
timeout = 300
[sync] # 模块名
comment = sync
# path为需要同步的文件夹路径
path = /root/sync
read only = no
list = yes
uid = root
gid = root
- 新建docker-compose.yml,内容如下
version: "2.2"
services:
centos:
image: ubuntu:latest
container_name: ubuntu
ports:
- "22:22" # 22是ssh端口
- "873:873" # 873是srync端口
cap_add:
- ALL
tty: true
将上述4个文件放入ubuntu文件夹内,打开 命令行,进入ubuntu文件夹内,输入如下命令:
docker build -t ubuntu:latest .
ubuntu:latest:标签名
.:dockerfile文件所在位置
等待docker镜像拉去完成
然后,输入如下命令:
docker-compose up -d
-d:新建的容器在后端运行
至此,c/c++的环境已经配置完成。
开发客户端连接开发环境
在clion客户端中打开如下设置项
然后:
其中,密码和端口分别在Dockerfile文件和docker-compose.yml文件中配置。
至此,已经完成Mac下docker中c/c++开发环境。
注意情况
- 如果clion中连接docker中ubuntu被拒绝。
修改ubuntu中的配置文件
sudo vim/etc/ssh/sshd_config
找到并用 #注释掉这行:PermitRootLogin prohibit-password // 允许root登录,但是禁止root用密码登录(默认值在文件中是被注释掉的)
新建一行 添加:PermitRootLogin yes //允许root登录,设为yes。
重启服务
sudo service ssh restart