Linux c++编译总结(持续更新)
2024-08-28 00:30:10
1. 没有定义的符号
这类的错误, 解决办法:A. 添加对应的头文件(源文件), B.前置声明
- 1.1 错误描述:
error: variable has incomplete type 'class XXX '
error: 'helper' is not a class, namespace, or enumeration
- 1.2 编译器说的很清楚,没有找到其定义, 看看错误的代码
class _utils_api_ helper
{
public:
};
- 1.3 错误提示:
[ 20%] Building CXX object CMakeFiles/calc.dir/src/utils.cpp.o
In file included from /home/xxx/demo/call_clang/src/utils.cpp:1:
/home/xx/demo/call_clang/include/utils.h:53:2: error: expected expression
public:
/home/cube/demo/call_clang/include/utils.h:51:20: error: variable has incomplete type 'class _utils_api_'
class _utils_api_ helper
^
/home/xx/demo/call_clang/include/utils.h:51:8: note: forward declaration of 'utils::_utils_api_'
class _utils_api_ helper
^
/home/xx/demo/call_clang/src/utils.cpp:14:14: error: 'helper' is not a class, namespace, or enumeration
std::string helper::wstr2str(const std::wstring& wstr)
- 1.4 分析,这里提示,没有定义
_utils_api_
, 而用_utils_api_
修饰了类helper - 1.5 解决: 增加对
_utils_api_
的定义。 - 1.6 扩展, 可能没有包含头文件就已经在使用头文件中的函数或者类型,也会出现这样的错误,解决: 增加头对应的类型引用
2. C++特有的与C一起编译
- 2.1 错误:
In file included from /home/xxx/demo/call_clang/src/utils.cpp:1:
/home/xxx/demo/call_clang/include/utils.h:149:3: error: templates must have C++ linkage
template<typename T>
^~~~~~~~~~~~~~~~~~~~
/home/xxxxxx/demo/call_clang/include/utils.h:42:2: note: extern "C" language linkage specification begins here
extern "C" {
^
/home/xxxxxx/demo/call_clang/include/utils.h:167:3: error: templates must have C++ linkage
template<typename T>
^~~~~~~~~~~~~~~~~~~~
/home/xxxxxx/demo/call_clang/include/utils.h:42:2: note: extern "C" language linkage specification begins here
extern "C" {
重要的是这句提示
error: templates must have C++ linkage
- 2.2 提示说: 模板是C++ 才有的,而我的代码是:
#ifdef __cplusplus
extern "C" {
#endif //! __cplusplus
class helper
{
public:
template<T>
std::string to_str(T & val)
{
return std::to_string(val);
}
};
#ifdef __cplusplus
}
#endif //! __cplusplus
代码中使用模板, 又使用extern "C"尝试兼容C语言,当然不行,C没有模板
最新文章
- ajax+php+mysql 实现点赞、局部刷新,每个IP只能对一篇文章点赞一次
- 第九章 springboot + mybatis + 多数据源 (AOP实现)(转载)
- (源码)自己写的ScrollView里套漂亮的圆角listview(算是漂亮吧。。。)
- android 中 listview 设置自动匹配高度
- ios 5
- jmeter接口测试之登录测试
- bzoj1965 [Ahoi2005]SHUFFLE 洗牌
- Largest Rectangle in Histogram 解答
- WebSocket API
- php intval()和floatval()
- SAP MM 明明有需求,为啥MRP RUN后没有PR单据产生?
- Codeforces.871D.Paths(莫比乌斯反演 根号分治)
- | 线段树-地平线horizon
- 结构型---代理模式(Proxy Pattern)
- springboot项目打成war包
- 深度学习原理与框架-Tfrecord数据集的读取与训练(代码) 1.tf.train.batch(获取batch图片) 2.tf.image.resize_image_with_crop_or_pad(图片压缩) 3.tf.train.per_image_stand..(图片标准化) 4.tf.train.string_input_producer(字符串入队列) 5.tf.TFRecord(读
- Deep learning with Python 学习笔记(10)
- 尚学堂java答案解析 第三章
- ASP.NET中常用输出JS脚本的类(来自于周公博客)
- cut的用法【转】