对"fun2()"的未定义引用
undefined reference to "fun2()"
我有a.c.b.cpp文件。
/****** a.c ******/
fun1(...)
{
..........
fun2(...); /* function present in b.cpp */
..........
}
/******* b.cpp *******/
extern "C" int fun2(...);
int fun2(...)
{
..........
}
我已经编译了如下代码:
a.o:a.c b.o
gcc -c -o a.o a.c b.o
b.o:b.cpp
g++ -c -o b.o b.cpp
但我得到的错误是未定义引用"fun2()"。这是正确的编译方式吗?还是我需要更改什么。?
您需要在a.c.中添加该功能的原型
extern "C" int fun2(...);
在b.cpp中有一行,这是定义函数的地方,告诉它使函数具有"C"链接,但在a.C中没有相应的原型来告诉它函数存在。
/****** a.c ******/
extern int fun2(...);
fun1(...)
{
..........
fun2(...); /* function present in b.cpp */
..........
}
会修复它。或者把它放在一个标题中
// b.h
#pragma once
// tell c++ you want these externs to have C linkage
#ifdef __cplusplus
extern "C" {
#endif
// list your "C" accessible functions here.
extern int fun2(...);
// end the extern scope
#ifdef __cplusplus
};
#endif
// a.c
#include "b.h"
// b.cpp
#include "b.h"
为什么要从对象文件构造对象文件?您不需要将b.o链接到a.o,只需从相应的c构建每个对象,并在最后链接所有内容(使用main()函数)。
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用