C++环境配置和编程技巧
# 开发环境配置
# 配置vim
更新apt源信息
sudo apt update
安装
git
sudo apt install git
配置vim,执行下面命令配置安装vim
wget 47.93.11.51:88/install_vim.sh bash install_vim.sh
vim的配置因为需要安装较多插件,所以需要等较多时间,请耐心等待
# zsh的安装及配置
- 安装zsh
sudo apt install zsh
- 修改默认shell为zsh
chsh -s /bin/zsh
- 安装oh-my-zsh
wget 47.93.11.51:88/install_zsh.sh
bash install_zsh.sh
- 安装==zsh-syntax-highlighting==
git clone https://gitee.com/suyelu/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
- 使用命令
vim ~/.zshrc
打开.zshrc文件,找到plugins=()
这一行,将zsh-syntax-highlighting添加进去
plugins=(git zsh-syntax-highlighting)
- 安装其他插件
##命令自动补全插件
mkdir ~/.oh-my-zsh/plugins/incr
wget http://mimosa-pudica.net/src/incr-0.2.zsh -O ~/.oh-my-zsh/plugins/incr/incr.plugin.zsh
##目录自动跳转插件
sudo apt install autojump
- 使用命令
vim ~/.zshrc
,打开后在最后插入以下内容:
#设置终端颜色,提示符,及上一条指令返回码提示
autoload -U colors && colors
PROMPT="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%1~ %{$reset_color%}%# "
RPROMPT="[%{$fg[yellow]%}%?%{$reset_color%}]"
# Useful support for interacting with Terminal.app or other terminal programs
[ -r "/etc/zshrc_$TERM_PROGRAM" ] && . "/etc/zshrc_$TERM_PROGRAM"
source /usr/share/autojump/autojump.sh
source ~/.oh-my-zsh/plugins/incr/incr*.zsh
注意,复制后可能会因为Vim的配置导致以上内容被注释,也就是在前面加上了
#
,如果有的话,删掉就行。
# ctags安装与配置
- 使用以下命令安装ctags
sudo apt install ctags
- 执行以下命令
ctags -I __THROW -I __attribute_pure__ -I __nonnull -I __attribute__ --file-scope=yes --langmap=c:+.h --languages=c,c++ --links=yes --c-kinds=+p --c++-kinds=+p --fields=+iaS --extra=+q -f ~/.vim/systags /usr/include/* /usr/include/x86_64-linux-gnu/sys/* /usr/include/x86_64-linux-gnu/bits/* /usr/include/arpa/*
- 使用命令
vim ~/.vimrc
编辑.vimrc,在最后添加以下内容
set tags+=~/.vim/systags
# 安装glibc-doc
- 使用以下命令安装
sudo apt install glibc-doc
# 在linux系统中安装VSCode(Visual Studio Code)
- 从官网下载压缩包(话说下载下来解压就直接可以运行了咧,都不需要make) 访问Visual Studio Code官网 https://code.visualstudio.com/docs?dv=linux64
wget https://az764295.vo.msecnd.net/stable/7ba55c5860b152d999dda59393ca3ebeb1b5c85f/code-stable-code_1.7.2-1479766213_amd64.tar.gz
- 解压 ,如果文件名不对,可能解压不出来的(扩展名:tar.gz)
tar jxcv code-stable-code_1.7.2-1479766213_amd64.tar.gz
- 然后移动到 /usr/local/ 目录
mv VSCode-linux-x64 /usr/local/
- 可能还需要给可执行的权限, 然后就已经可以运行了
chmod +x /usr/local/VSCode-linux-x64/code
- 复制一个VScode图标文件到 /usr/share/icons/ 目录(后面会有用)
cp /usr/local/VSCode-linux-x64/resources/app/resources/linux/code.png /usr/share/icons/
- 创建启动器, 在/usr/share/applications/ 目录, 也可以将它复制到桌面目录 直接在中断 使用 命令:
vim /usr/share/applications/VSCode.desktop
然后输入以下文本:
[Desktop Entry]
Name=Visual Studio Code
Comment=Multi-platform code editor for Linux
Exec=/usr/local/VSCode-linux-x64/code
Icon=/usr/share/icons/code.png
Type=Application
StartupNotify=true
Categories=TextEditor;Development;Utility;
MimeType=text/plain;
保存后退出, 然后可以复制到桌面:
cp /usr/share/applications/VSCode.desktop ~/桌面/
之后 就会发现 桌面和 应用程序菜单都有了 VSCode的快捷方式了
8.打开VSCode, 加载插件: cpptools | vscode-icons
有时候打不开,键入如下命令:
sudo apt-get install libgconf-2-4
sudo nautilus 打开文件
# 项目编译
- 当自定义文件用<>包含时,需要将自定义文件通过-I的形式进行编译,即g++ -I./ main.cpp
- include 头文件
- src 源文件
- bin 可执行文件
- lib 链接库文件
- g++ -I./inclde -c ./src/head1.cc 编译
- g++ *.o 链接
# makefile
touch makefile
.PHONY: clean run
all: main.o ./src/head1.o ./src/head2.o
g++ main.o ./src/head1.o ./src/head2.o -o ./bin/KKB
main.o: main.cpp ./include/head1.h ./include/head2.h
g++ -I./include -c main.cpp
./src/head1.o: ./src/head1.cc ./inlude/*.h
g++ -I./include ./src/head1.cc -o ./src/head1.o
./src/head2.o: ./src/head2.cc ./inlude/*.h
g++ -I./include ./src/head2.cc -o ./src/head2.o
clean:
rm ./bin/KKB ./src/*.o mian.o
run:
./bin/KKB
make
make clean
make run
建立依赖关系
# 链接库
静态 .a
动态 .so
创建静态链接库 ar -r libxxx.a head1.o head2.o
g++ -I./include main.cpp -L./lib -l xxx
# 如何写makefile
前言:源文件首先会生成中间目标文件,再由中间目标文件生成执行文件。
make命令执行时,需要一个 Makefile 文件,以告诉make命令需要怎么样的去编译和链接程序。
# makefile的规则
target ... : prerequisites ...
command
...
...
target
可以是一个object file(目标文件),也可以是一个执行文件,还可以是一个标签(label)。
prerequisites
生成该target所依赖的文件和/或target,prerequisites中如果有一个以上的文件比target文件要新的话,command所定义的命令就会被执行。
command
该target要执行的命令(任意的shell命令),一定要以一个 Tab 键作为开头