可能重复:
G ++ Cpp中的"未定义引用"
我有这个标题
#ifndef TEST_H_
#define TEST_H_
class TEST
{
public :
TEST();
};
#endif /* TEST_H_ */
Run Code Online (Sandbox Code Playgroud)
和这个标题
#ifndef TESTS_H_
#define TESTS_H_
#include "TEST.h"
class TESTS : public TEST
{
public :
TESTS();
};
#endif /* TESTS_H_ */
Run Code Online (Sandbox Code Playgroud)
我实现了这样的标题:
#include <iostream>
using namespace std;
#include "TEST.h"
TEST:: TEST()
{
}
int main(int argc, char **argv) {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
还有这个:
#include "TESTS.h"
TESTS :: TESTS() : TEST()
{
}
int main(int argc, char **argv) {
return 0; …Run Code Online (Sandbox Code Playgroud) 我有一个简单的程序,我完全从http://www.learncpp.com/cpp-tutorial/19-header-files/ 中的示例中复制了该程序,因为我正在学习如何使用多个文件制作 C++ 程序。
程序可以编译,但是在构建时,出现以下错误:
/tmp/ccm92rdR.o: 在函数 main: main.cpp:(.text+0x1a): 对 `add(int, int)' 的未定义引用 collect2: ld 返回 1 个退出状态
这是代码:
主程序
#include <iostream>
#include "add.h" // this brings in the declaration for add()
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
添加.h
#ifndef ADD_H
#define ADD_H
int add(int x, int y); // function prototype for add.h
#endif
Run Code Online (Sandbox Code Playgroud)
添加.cpp
int add(int x, int y) …Run Code Online (Sandbox Code Playgroud) struct FailedTransaction{
OrderNodePtr order;
int failureID;
struct FailedTransaction* next;
struct FailedTransaction* tail;
};
typedef struct FailedTransaction* FailedTransactionPtr;
struct SuccessfulTransaction{
OrderNodePtr order;
struct SuccessfulTransaction* next;
struct SuccessfulTransaction* tail;
};
typedef struct SuccessfulTransaction* SuccessfulTransactionPtr;
struct FinalReport{
FailedTransactionPtr failedTransactions;
SuccessfulTransactionPtr successfulTransactions;
};
struct FinalReport* report = NULL;
Run Code Online (Sandbox Code Playgroud)
这段代码在 main 之上声明。访问时
report->successfulTransactions
Run Code Online (Sandbox Code Playgroud)
或者
report->failedTransactions
Run Code Online (Sandbox Code Playgroud)
我得到了 FailedTransaction 和SuccessfulTransaction 的未定义标识符。
这是操作报告的代码
if(report == NULL){
report = malloc(sizeof(struct FinalReport));
report->failedTransactions = NULL;
report->successfulTransactions = NULL;
}
if(outcome){
if(report->successfulTransactions == NULL){
report->successfulTransactions = malloc(sizeof(SuccessfulTransaction));
report->successfulTransactions->order = temp;
report->successfulTransactions->tail …Run Code Online (Sandbox Code Playgroud) 我正在学习如何用 C 语言制作 Windows 应用程序,并尝试使用SetPixel(HDC hDC, int x, int y, COLORREF crColor).
在我添加该行之前,我从未遇到过编译错误。当然,我确实包含了 windows.h 。我四处寻找答案,但一无所获。
它给出了错误" undefined reference to _imp__SetPixel@16' "
为了编译,我使用命令" gcc -g -Wall -o $(EXENAME) $(SRC) "。没有花哨的 IDE。如果有人能告诉我为什么是imp_和 @16 部分,请也告诉我。我以前从未见过这样的情况。
编辑:我在这个网站上找到了 _imp 的含义。
最近,我的自定义编写的通用矢量代码遇到了一些问题,该代码依赖于功能模板。我不愿意将实现包含在头文件中(这在模板中很常见),因为这会显着增加编译时间。因此,我在 .cpp 文件中手动实例化所需的类。然而,这仍然会导致未定义的参考错误。我已将代码缩减为以下代码片段,但仍然会生成错误:
矩阵d.cpp
#include "matrixd.h"
namespace math
{
template class _vec2<float>;
template<class T> _vec2<T>::_vec2() {}
}
Run Code Online (Sandbox Code Playgroud)
矩阵d.h
#pragma once
namespace math
{
template <class T>
class _vec2
{
public:
T x, y;
_vec2<T>();
void reset();
};
typedef _vec2<float> vec2;
}
Run Code Online (Sandbox Code Playgroud)
测试.cpp
#include "matrixd.h"
int main()
{
math::_vec2<float> v;
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
在函数
main': source.cpp:(.text+0x10): undefined reference tomath::_vec2::_vec2()'collect2: 错误: ld 返回 1 退出状态
任何帮助,将不胜感激!:)
在一个 C++ 项目中,我试图调用这个在 C 中定义的函数:
int CyBtldr_RunAction(CyBtldr_Action action, const char* file, const uint8_t* securityKey,
uint8_t appId, CyBtldr_CommunicationsData* comm, CyBtldr_ProgressUpdate* update);
Run Code Online (Sandbox Code Playgroud)
CyBtldr_ProgressUpdate 也在 C 中定义为:
typedef void CyBtldr_ProgressUpdate(uint8_t arrayId, uint16_t rowNum);
Run Code Online (Sandbox Code Playgroud)
我收到以下未定义的参考错误。我错过了什么吗?
.\bootloader.cpp:88: error: undefined reference to 'CyBtldr_RunAction(CyBtldr_Action, char const*, unsigned char const*, unsigned char, CyBtldr_CommunicationsData*, void (*)(unsigned char, unsigned short))'
Run Code Online (Sandbox Code Playgroud) 我最近刚刚通过Cygwin64将GCC安装到Windows上。我正在尝试编译一个包含Windows _ftprintf函数的程序(如 MyHandleError函数中的此处所示,并在此处记录),但是,我得到的是未定义的引用。
例如:
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
void main(void){
_ftprintf(stderr, TEXT("Test Err. \n"));
}
Run Code Online (Sandbox Code Playgroud)
以下示例在编译为时产生以下错误gcc test.c:
test.c: In function 'main':
test.c:6:5: warning: implicit declaration of function '_ftprintf'; did you mean '__eprintf'? [-Wimplicit-function-declaration]
_ftprintf(stderr, TEXT("Test Err. \n"));
^~~~~~~~~
__eprintf
/cygdrive/c/Users/Dan/AppData/Local/Temp/ccEa2phm.o:test.c:(.text+0x21): undefined reference to `_ftprintf'
/cygdrive/c/Users/Dan/AppData/Local/Temp/ccEa2phm.o:test.c:(.text+0x21): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `_ftprintf'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我想念国旗吗?
我注意到当我使用sin内部函数时,编译器无法识别它,这是一个例子:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
float sinus(float a){
return sin(a);}
int main(int argc, char **argv)
{
double a = sinus(2);
printf("%f \n", sin(2));
printf("%f", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我直接在 main 中使用它,它工作正常,但在用户定义的函数中,它给了我这个错误undefined reference to sin。
对于编译,我使用gcc -Wall -lm -lc -lgcc -o "%e" "%f".
这是一些重现该问题的简单代码:
#include <box2d/box2d.h>
int main()
{
b2World world(b2Vec2_zero);
b2BodyDef bdef;
b2Body* body = world.CreateBody(&bdef);
body->SetUserData(body);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这应该根据我读过的所有内容进行编译,并且(对于学究来说)我猜它在技术上确实可以编译,但是当我尝试(使用g++ test.cpp -lbox2d)时,我收到链接器错误:
/usr/bin/ld: /tmp/ccgHfvqv.o: in function `main':
test.cpp:(.text+0x75): undefined reference to `b2Body::SetUserData(void*)'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我尝试用谷歌搜索,"undefined reference to b2Body::SetUserData(void*)"但没有找到结果。
我也尝试通过设置它b2BodyDef,但userData该结构中的成员似乎工作方式不同,指向b2BodyUserData具有单个pointer成员的结构,该结构似乎没有设计用于保存用户数据指针,因为在那里设置地址会导致Box2D 稍后写入该地址,从而损坏数据。(我使用 GDB 观察点来检查这一点。)
我有这个Java代码:
public interface Adapter<FROM,TO> {
/**
* Adapts something from one type to a type.
* @param f
* @return
*/
public TO adapt(FROM f);
/**
* Chains adapters.
*
* @param <NEXT>
* @param next
* @return a new adapter that takes the output of this adapter and
* adapts to something of type NEXT.
*/
public default <NEXT> Adapter<FROM,NEXT> chain(Adapter<TO,NEXT> next){
Adapter<FROM,TO> x=this;
return new Adapter<FROM,NEXT>(){
@Override
public NEXT adapt(FROM f) {
return next.adapt(x.adapt(f));
}
};
}
Run Code Online (Sandbox Code Playgroud)
我真的很喜欢 …