Linux C++(不断更新)

开发环境搭建

sudo apt update
sudo apt install cmake gcc clang gdb build-essential

GCC

g++ 编译参数

1、-pedantic 选项,那么使用了扩展语法的地方将产生相应的警告信息

2、-Wall 使用它能够使GCC产生尽可能多的警告信息

3、-Werror,它要求GCC将所有的警告当成错误进行处理

https://zhuanlan.zhihu.com/p/393419013

应该始终使用 -Wall 和 -Wextra 进行编译,因为它们会启用警告并帮助我们以了解我们的错误,如果有的话。

还可以加 -c 表示只编译不链接,比如一套完整流程:

g++ -E test.cpp -o test.i
g++ -S test.i -o test.s
g++ -c test.s -o test.o
g++ test.o -o test

一般测试:

clang++ -Wall -Wextra -std=c++20 test.cc
./a.out
g++ -Wall -Wextra -Werror -pedantic -std=c++20 test.cc
./a.out
g++ test.cc -std=c++17 -W{all,extra,error,pedantic}
./a.out

-Werror :貌似直接把warning当作error处理。

-pedantic :源程序中使用了扩展语法的地方将产生相应的警告信息。

g++ 版本

sudo apt install g++-10
image

切换默认版本

https://askubuntu.com/questions/26498/how-to-choose-the-default-gcc-and-g-version

  1. Installed the new desired version:
sudo apt install gcc-10
sudo apt install g++-10
  1. Switch to the new version:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10
  1. Test with:
gcc --version
g++ --version

连接 boost

sudo apt install libboost-all-dev

测试:(参考 https://sfumecjf.github.io/cmake-examples-Chinese/01-basic/1.7 Including Third Party Library.html

main.cpp:

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/filesystem.hpp>

int main(int argc, char *argv[])
{
    std::cout << "Hello Third Party Include!" << std::endl;

    // use a shared ptr
    boost::shared_ptr<int> isp(new int(4));

    // trivial use of boost filesystem
    boost::filesystem::path path = "/usr/share/cmake/modules";
    if(path.is_relative())
    {
        std::cout << "Path is relative" << std::endl;
    }
    else
    {
        std::cout << "Path is not relative" << std::endl;
    }

   return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

# Set the project name
project (third_party_include)


# find a boost install with the libraries filesystem and system
find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)

# check if boost was found
if(Boost_FOUND)
    message ("boost found")
else()
    message (FATAL_ERROR "Cannot find Boost")
endif()

# Add an executable
add_executable(third_party_include main.cpp)

# link against the boost libraries
target_link_libraries( third_party_include
    PRIVATE
        Boost::filesystem
)

注:

几乎所有不平凡的项目都将要求包含第三方库,头文件或程序。 CMake支持使用find_package()函数查找这些工具的路径。 这将从CMAKE_MODULE_PATH中的文件夹列表中搜索格式为“ FindXXX.cmake”的CMake模块。 在linux上,默认搜索路径将是/ usr / share / cmake / Modules。 在我的系统上,这包括对大约142个通用第三方库的支持。

此示例要求将Boost库安装在默认系统位置。

VSCODE 篇

VSCODE CMAKE 查找Kit

Scan for Kits 后可以选择一个 Kit 比如 GCC,想换的时候选 Select a Kit:
在这里插入图片描述
在最下面这里会显示当前 Kit:
在这里插入图片描述

Ubuntu

OpenGL

https://blog.csdn.net/renhaofan/article/details/82631082

Ubuntu 搜索包:apt search xxx

更新包

sudo apt upgrade cmake
https://blog.csdn.net/zhjulia123/article/details/83479515

apt update:只检查,不更新(已安装的软件包是否有可用的更新,给出汇总报告)
用法:sudo apt update
apt upgrade:更新已安装的软件包
用法:sudo apt upgrade 软件包名

虚拟机

网络问题

https://askubuntu.com/questions/1267043/virtual-machine-ubuntu-20-04-lts-connect-network-is-unreachable

指令:sudo dhclient ens33

原理参考:https://zhuanlan.zhihu.com/p/353773830

sudo dhclient ens33#获取ip
sudo ifconfig ens33#查看ip

dhclient 命令可以根据网卡名称,动态分配内存;

WSL2

Connection failed [IP: 91.189.91.39 80]

折腾半天都无法让 wsl2 连上vpn,导致下载包出错,最后只好这样解决:

先手动下载到一个文件夹,然后挪一下路径(来让 apt 找得到,参考:
https://blog.csdn.net/weixin_44120025/article/details/122317808?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_aa&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_aa&utm_relevant_index=1

sudo mv libfaad2_2.9.1-1_amd64.deb /var/cache/apt/archives
再下载:
sudo apt install vlc

运行 GUI 程序

https://docs.microsoft.com/en-us/windows/wsl/tutorials/gui-apps
https://github.com/microsoft/wslg

posted @ 2022-03-10 16:48  hebh  阅读(420)  评论(0编辑  收藏  举报