Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the knowledge points related to the development of linux C++

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

本篇内容主要讲解"linux C++开发相关知识点有哪些",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"linux C++开发相关知识点有哪些"吧!

一、CMake 常用命令

开发中使用CMake进行编译和生成 ,所以需要先熟悉 CMake 相关命令 。在项目编译目录下创建 CMakeLists.txt (固定格式,不能出现拼写错误)文件,然后编辑 CMake 文件 。以本次开发中的 CMakeLists.txt为例:

cmake_minimum_required(VERSION 3.10)project(xxx)# SET(CMAKE_BUILD_TYPE "Debug") Release 等SET(CMAKE_BUILD_TYPE "MinSizeRel")add_compile_options(-fPIC)include(CheckCXXCompilerFlag)CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)if(COMPILER_SUPPORTS_CXX11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")elseif(COMPILER_SUPPORTS_CXX0X) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive")set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -rdynamic")if(TARGET_A) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTARGET_A")elseif(TARGET_B) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTARGET_B")endif()set(CMAKE_C_COMPILER "/xxxxx/xxxx/bin/arm-openwrt-linux-gcc")set(CMAKE_CXX_COMPILER "/xxxx/xxxxx/xxxxx/bin/arm-openwrt-linux-g++")aux_source_directory(xxxx/common/ SRC_COMMON)aux_source_directory(xxxx/test/ SRC_TEST)# 头文件搜索目录include_directories( /usr/local/include /xxxxxx交叉编译工具链头文件目录/usr/include /其他三方法库头文件目录/include )ADD_LIBRARY(AAAA SHARED ${SRC_COMMON} ${SRC_TEST})# 链接动态库target_link_libraries(AAAA /xxxxx动态库路径/libprotobuf.so/home/xxx/Desktop/xxx/libxxxx.so /usr/local/lib/libxxxx.so)# 设置动态库安装路径INSTALL(TARGETS AAAA LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)INSTALL(DIRECTORY cxx_api/exportc DESTINATION include)

CMake 具体命令参考:https://my.oschina.net/zhxx/blog/4511944

编译时在 CMakeLists.txt目录下 mkdir build -> cd build -> cmake .. - > 编译成功后 make ->sudo make install 动态库就会被安装到相应的目录下 。

项目中用到的 CMake 命令Demo :

cmake .. -DCMAKE_CXX_COMPILER:FILEPATH=/xxxx/xxxxx/bin/arm-openwrt-linux-g++ -DCMAKE_C_COMPILER:FILEPATH=/xxxxx/xxxxxx/bin/arm-openwrt-linux-gcc -DCOMPILER_SUPPORTS_CXX11=ture -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON

-D 可以用来增加编译参数 ,如其中的 -DCOMPILER_SUPPORTS_CXX11=ture ,CMake 中

if(COMPILER_SUPPORTS_CXX11)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

条件成立,执行 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

注意:链接的动态库需要保证所有动态库的格式是一致的 。

1.查看动态库格式及其他信息命令 : readelf -h xxx.so

2.查看动态库链接的其他动态库命令: readelf -d xxx.so

3.动态库压缩:从对应的交叉编译工具链中搜索 strip 工具,然后将工具拖到命令行 ,后面跟上要压缩的动态库路径,即可极大程度上的压缩动态库大小,

二、配置 c_cpp_properties.json 文件

配置好之后,项目中的头文件才能点进去看具体的实现,写代码的时候才会有代码提示, 也不会再出现一大堆飘红的错误 。

{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}**", "other headers paths" ....... "/usr/include/c++/7", "/usr/include/x86_64-linux-gnu/c++/7", "/usr/include/c++/7/backward", "/usr/lib/gcc/x86_64-linux-gnu/7/include", "/usr/local/include", "/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed", "/usr/include/x86_64-linux-gnu", "/usr/include/**", "/home/xxxx/xxxxxx/include/c++/4.8.3/*" ], "defines": [], # 交叉编译工具路径 "compilerPath": "/home/xxxxxx/bin/aarch74-openwrt-linux-g++", "cStandard": "c11", "cppStandard": "c++11", "intelliSenseMode": "gcc-arm", "configurationProvider": "ms-vscode.cmake-tools" } ], "version": 4}三、调试

生成的动态库和测试程序需要放到无屏音响进行测试 。本次开发中使用的是通用的 ssh 方法。

ssh 环境配置好以后 , ssh 主机名 连接到到音响

使用 scp 将 pc 上的动态库拷贝到音响上 如:scp -r /home/xxx/Desktop/so/ 音响主机名:/tmp/so 将桌面 so 文件夹下的动态库拷贝到音箱 /tmp/so 文件夹下

scp main 音响主机名:/tmp 将main 可执行程序拷贝到 音箱tmp 目录下 。

四、执行

由于是在main函数中写的测试程序,方法走完测试程序就退出了 ,所以就用 另起一个线程把测试程序放在该线程执行 ,然后一直 sleep 当前线程的方法来进行测试的 。

由于动态库并没有拷贝到默认的动态库搜索目录下,所以需要指定动态库搜索目录:

export LD_LIBRARY_PATH=/tmp/so

ssh 音箱主机名, 连接到音箱 cd ../tmp 目录下 ./main 执行 。

到此,相信大家对"linux C++开发相关知识点有哪些"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report