# vscode default extension
在根目录下添加一个 c_cpp_properties.json
, 然后在 includePath 属性中添加目标目录.
{ | |
"configurations": [ | |
{ | |
"name": "Linux", | |
"includePath": [ | |
"${workspaceFolder}", | |
"${workspaceFolder}/include" | |
], | |
"defines": [], | |
"compilerPath": "/usr/bin/clang", | |
"cStandard": "c11", | |
"cppStandard": "c++11", | |
"intelliSenseMode": "linux-clang-x64" | |
} | |
], | |
"version": 4 | |
} |
# vscode clangd
clangd 对代码解析的速度比官方插件快很多,但是用的人较少,导致出现问题时资料比价少。
如果是 CMake 项目请参考下面👇,非 CMake 项目添加 include path 需要在 settings.json
中添加如下内容
"clangd.fallbackFlags": [ | |
"-I${workspaceFolder}/include", | |
"-I${workspaceFolder}" | |
] |
# CMake
include_directories("/usr/src/linux-headers-6.8.0-51-generic/include") # 添加头文件路径 | |
LINK_DIRECTORIES("/usr/src/linux-headers-6.8.0-51-generic/lib") # 添加库路径 | |
LINK_LIBRARIES(test lib1 lib2) # 链接库,无需.lib 后缀 |
# shell
方法一对于 gcc 和 clang 是通用的,方法二只需要替换对应应用即可 gcc/g++/clang/clang++
。
# 方法一:修改环境变量
(临时修改)直接在当前终端中执行如下命令
export C_INCLUDE_PATH=/usr/local/C_IncludeDir:$C_INCLUDE_PATH # 把某 include 目录加到 C 语言系统目录 | |
export CPLUS_INCLUDE_PATH=/usr/local/CppIncludeDir:$CPLUS_INCLUDE_PATH # 把某 include 目录加到 C++ 语言系统目录 | |
export LIBRARY_PATH=/usr/local/yourLibDir:$LIBRARY_PATH # 添加某静态库目录 | |
export LD_LIBRARY_PATH=/usr/local/yourLdLibDir:$LD_LIBRARY_PATH # 添加某动态库目录 |
(永久修改)将上述内容添加到使用的 shell 的配置文件中:
- zsh:
~/.zshrc
- bash:
~/.bashrc
- 想对所有用户生效:
/etc/profile
(需重新登录)
修改完成后可以使用如下命令查看是否修改成功:
- C:
echo | gcc -v -x c -E -
- C++:
echo | g++ -v -x c++ -E -
输出类似:
❯ echo | gcc -v -x c -E - | |
Using built-in specs. | |
COLLECT_GCC=gcc | |
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa | |
OFFLOAD_TARGET_DEFAULT=1 | |
Target: x86_64-linux-gnu | |
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 13.3.0-6ubuntu2~24.04' --with-bugurl=file:///usr/share/doc/gcc-13/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-13 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/libexec --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-libstdcxx-backtrace --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-13-fG75Ri/gcc-13-13.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-13-fG75Ri/gcc-13-13.3.0/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2 | |
Thread model: posix | |
Supported LTO compression algorithms: zlib zstd | |
gcc version 13.3.0 (Ubuntu 13.3.0-6ubuntu2~24.04) | |
COLLECT_GCC_OPTIONS='-v' '-E' '-mtune=generic' '-march=x86-64' | |
/usr/libexec/gcc/x86_64-linux-gnu/13/cc1 -E -quiet -v -imultiarch x86_64-linux-gnu - -mtune=generic -march=x86-64 -fasynchronous-unwind-tables -fstack-protector-strong -Wformat -Wformat-security -fstack-clash-protection -fcf-protection -dumpbase - | |
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu" | |
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/13/include-fixed/x86_64-linux-gnu" | |
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/13/include-fixed" | |
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/13/../../../../x86_64-linux-gnu/include" | |
#include "..." search starts here: | |
#include <...> search starts here: | |
/usr/lib/gcc/x86_64-linux-gnu/13/include | |
/usr/local/include | |
/usr/include/x86_64-linux-gnu | |
/usr/include | |
End of search list. | |
# 0 "<stdin>" | |
# 0 "<built-in>" | |
# 0 "<command-line>" | |
# 1 "/usr/include/stdc-predef.h" 1 3 4 | |
# 0 "<command-line>" 2 | |
# 1 "<stdin>" | |
COMPILER_PATH=/usr/libexec/gcc/x86_64-linux-gnu/13/:/usr/libexec/gcc/x86_64-linux-gnu/13/:/usr/libexec/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/13/:/usr/lib/gcc/x86_64-linux-gnu/ | |
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/13/:/usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/13/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/13/../../../:/lib/:/usr/lib/ | |
COLLECT_GCC_OPTIONS='-v' '-E' '-mtune=generic' '-march=x86-64' |
include
下面的就是 gcc/g++ 搜索头文件的路径, LIBRARY_PATH
后是搜索库的路径。
# 方法二:添加到编译命令
对于 C: gcc -I/home/user/my_include -L/home/user/my_lib -lmycustomlib main.c
对于 C++: g++ -I/home/user/my_include -L/home/user/my_lib -lmycustomlib main.cpp
-I
:指定头文件搜索路径/home/user/my_include
-L
:指定库搜索路径/home/user/my_lib
-l
:使用-l<库名>
指定库名时,gcc
会自动加上lib
前缀和.a
或.so
后缀。例如:-lmycustomlib
寻找的是libmycustomlib.a
或libmycustomlib.so
。
# Makefile
设置 CPPFLAGS
或 CFLAGS
来将编译命令传递给编译器,命令和用法与「命令行编译的方法二」含义一致,一个 makefile 示例如下:
CC = gcc | |
# 添加头文件搜索路径和库搜索路径 | |
CPPFLAGS += -Iinclude -Iinclude2 # 添加了两个头文件搜索路径 | |
LDFLAGS += -Llib -L/usr/local/lib -lmycustomlib # 添加了两个库搜索路径并指定链接 mycustomlib 库 | |
# 源文件和目标文件 | |
SRC = src/main.c | |
OBJ = main.o | |
TARGET = myprogram | |
# 编译规则 | |
$(TARGET): $(OBJ) | |
$(CC) -o $@ $^ $(LDFLAGS) | |
%.o: %.c | |
$(CC) -c $< -o $@ $(CPPFLAGS) | |
clean: | |
rm -f $(OBJ) $(TARGET) |
如果编译过程有问题想检查搜索路径,可以在 Makefile
的规则中添加 -v
选项查看编译器的完整路径搜索信息:
$(CC) -v -o $@ $^ $(LDFLAGS) |
# CLion
CLion 默认使用 CMake 管理项目,请参考 CMake 部分
如果是导入 CLion 的项目没有使用 CMake 管理:
- (缺失头文件较少或是第三方库)可以使用
#include
后跟绝对路径的方式来找到对应的头文件。 - (缺失头文件较多)配置 makefile 来管理项目。