使用类进行链接时发生简单的C++未定义引用错误
Simple C++ undefined reference error during linking while using a class
我有一个简单的问题。但由于我是C++编程的新手,我无法解决这个问题。我创建了一个新的C++项目,使代码尽可能简短(因为原始代码要长得多),同时保留我遇到的问题。我在Stackoverflow和谷歌上搜索了大约50个相关问题,但到目前为止,没有任何东西能帮助我解决它。把所有东西都放在一个cc和一个h文件中是有效的,但不是我喜欢做的。提前感谢您的帮助。
我使用的是Ubuntu 14.04/Code::Blocks 13.12/gcc和g++4.8.2
问题是,我想访问在不同文件中定义的类中的函数,并且在编译工作时(由于file1.h中的extern)链接失败。如果我只是在file1.h中放入"Simple S1;"而不是"extern Simple S1!",并将其从file1.cc中删除,我会得到一个预期的多重声明错误。显然,"extern"技巧不适用于类,而适用于变量。
file1.h:
#ifndef FILE1_H
#define FILE1_H
class Simple
{
private:
unsigned int length = 10;
public:
void SetLength(unsigned int l) {length = l;}
unsigned int GetLength() {return length;}
};
extern Simple S1;
#endif
file1.cc:
#include <iostream>
#include "file1.h"
#include "file2.h"
int main()
{
Simple S1;
unsigned int l = GetL();
std::cout << "length=" << l << "n";
l = 20;
l = GetL();
std::cout << "length=" << l << "n";
return 0;
}
file2.h:
#ifndef FILE2_H
#define FILE2_H
unsigned int GetL();
#endif
file2.cc:
#include "file1.h"
#include "file2.h"
unsigned int GetL()
{
return S1.GetLength();
}
构建命令和错误:
g++ -std=c++11 -Wall -fexceptions -g -c file1.cc -o obj/Debug/file1.o
g++ -std=c++11 -Wall -fexceptions -g -c file2.cc -o obj/Debug/file2.o
g++ -o bin/Debug/Test obj/Debug/file1.o obj/Debug/file2.o
obj/Debug/file2.o: In function `GetL()':
file2.cc:6: undefined reference to `S1'
collect2: error: ld returned 1 exit status
如果S1是全局的,它必须在全局范围内定义,而不是在main()内定义,main()会创建该函数的本地新实例。
在函数中,将Simple S1;
放在main()之前。
#include <iostream>
#include "file1.h"
#include "file2.h"
Simple S1;
int main()
{
unsigned int l = GetL();
std::cout << "length=" << l << "n";
l = 20;
l = GetL();
std::cout << "length=" << l << "n";
return 0;
}
extern Simple S1;
该声明承诺在代码中的其他地方有一个类型为Simple
的全局对象S1
。但你从来没有定义过这样一个对象。最接近的是main()
中的局部变量S1
,但当然,这是完全不同的。
因此,只需将S1
的定义移出main()
:
Simple S1;
int main()
{
unsigned int l = GetL();
std::cout << "length=" << l << "n";
l = 20;
l = GetL();
std::cout << "length=" << l << "n";
return 0;
}
相关文章:
- 在c++中用vector填充一个简单的动态数组
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的简单if-else语句是如何无法访问的代码
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 一种在C++中读取TXT配置文件的简单方法
- 关于简单C++函数(is_palindrome)的逻辑的问题
- 显示错误输出的简单数组排序程序
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 退出简单while循环时出现问题
- 为什么简单的算术减法在"if"条件下不起作用?
- C++-字符串是否包含一个带有简单循环的单词
- 关于 c++ 函数中指针赋值的简单问题
- 从函数返回任意简单类型的数据
- 如何在没有函数的情况下编写此代码并使C++更简单?
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- CPU 瓶颈;处理具有许多非静态对象的 3D 场景渲染的简单方法