一个配置 g++ protobuf 环境的例子,以及g++常用参数 -I, -L, -l举例说明

protocxxx/yy.proto 编译为 yy.pb.ccyy.pb.h 之后 #include 导入会存在一些问题

这样编译可以成功:

g++ 
-fdiagnostics-color=always 

-g                # Generate source-level debug information 生成源代码级别的调试信息
xxx/lane_cpp.cpp 
xxx/numpy2cpp.cpp 
xxx/yewu.cpp 
xxx/yy.pb.cc 

-o                 # 输出可执行文件的名字
xxx/lane_cpp  

# 关于 -L,-l,-I 请继续往下看

-L/usr/local/lib 
-lcnpy 
-lz 

-L/opt/homebrew/Cellar/protobuf/21.5/lib 
-lprotobuf  

-I /opt/homebrew/include
-I /opt/homebrew/Cellar/protobuf/21.5/include 

--std=c++17      # C++ 17 标准

两个-I 选一个就行,因为一个google文件夹是另一个google文件夹的软链接:

xxx@xx include % ls -al
total 0
drwxrwxr-x   4 rx  admin  128  9  7 21:18 .
drwxrwxrwx  31 rx  admin  992  9  7 21:16 ..
-rw-r--r--   1 rx  admin    0  9  7 21:18 .keepme
lrwxr-xr-x   1 rx  admin   38  9  7 21:18 google -> ../Cellar/protobuf/21.5/include/google

xxx@xx include % pwd
/opt/homebrew/include

两个-I 都不加,会有:

In file included from x.cpp:19:
y.pb.h:10:10: fatal error: 'google/protobuf/port_def.inc' file not found
#include <google/protobuf/port_def.inc>
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

In file included from y.pb.cc:4:
y.pb.h:10:10: fatal error: 'google/protobuf/port_def.inc' file not found
#include <google/protobuf/port_def.inc>
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

一些 include 的位置会找不到,-I 是指定用来找 include 文件的路径的


不加 -lprotobuf 会报一系列的错,我放一部分:

Undefined symbols for architecture arm64:
  "google::protobuf::RepeatedField<float>::InternalSwap(google::protobuf::RepeatedField<float>*)", referenced from:
  
...

google::protobuf::Message::Message() in xxxx.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

不加 -L/opt/homebrew/Cellar/protobuf/21.5/lib 会找不到 protobuf

ld: library not found for -lprotobuf
clang: error: linker command failed with exit code 1 (use -v to see invocation)

-L 用来指定刚刚 -lprotobuf 的位置

看下这个路径 /opt/homebrew/Cellar/protobuf/21.5/lib

在这里插入图片描述

  • .a 应该是MAC的静态库
  • .dylib 应该是MAC的动态库
  • (Windows系统的动态库是DLL文件,Linux系统是so文件)

本文参考了这篇文章:
https://stackoverflow.com/questions/6141147/how-do-i-include-a-path-to-libraries-in-g

这篇文章把-I, -L, -l给说清楚了,nice!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值