编译 C++在Linux中
我正在尝试在Linux 中编译一个简单的应用程序。我的 main.cpp 看起来像
#include <string>
#include <iostream>
#include "Database.h"
using namespace std;
int main()
{
Database * db = new Database();
commandLineInterface(*db);
return 0;
}
其中 Database.h 是我的标头,并且有一个相应的 Database.cpp。编译时出现以下错误:
me@ubuntu:~/code$ g++ -std=c++0x main.cpp -o test
/tmp/ccf1PF28.o: In function `commandLineInterface(Database&)':
main.cpp:(.text+0x187): undefined reference to `Database::transducer(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0x492): undefined reference to `Database::transducer(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0x50c): undefined reference to `Database::transducer(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccf1PF28.o: In function `main':
main.cpp:(.text+0x721): undefined reference to `Database::Database()'
collect2: ld returned 1 exit status
正如您可以想象的那样,像这样的搜索到处都是。关于我可以采取哪些措施来解决该问题有什么建议吗?
I'm trying to compile a simple application in linux. My main.cpp looks something like
#include <string>
#include <iostream>
#include "Database.h"
using namespace std;
int main()
{
Database * db = new Database();
commandLineInterface(*db);
return 0;
}
Where Database.h is my header and has a corresponding Database.cpp. I get the following error when compiling:
me@ubuntu:~/code$ g++ -std=c++0x main.cpp -o test
/tmp/ccf1PF28.o: In function `commandLineInterface(Database&)':
main.cpp:(.text+0x187): undefined reference to `Database::transducer(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0x492): undefined reference to `Database::transducer(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0x50c): undefined reference to `Database::transducer(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccf1PF28.o: In function `main':
main.cpp:(.text+0x721): undefined reference to `Database::Database()'
collect2: ld returned 1 exit status
Searches for something like this are all over the place as you can imagine. Any suggestions on what I might be able to do to fix the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这些是链接器错误。它抱怨是因为它试图生成最终的可执行文件,但它不能,因为它没有
Database
函数的目标代码(编译器不会推断出与Database.txt 对应的函数定义)。 h
位于Database.cpp
中)。试试这个:
或者:
Those are linker errors. It is complaining because it is trying to produce the final executable, but it cannot, because it has no object code for
Database
functions (the compiler does not infer that the function definitions corresponding toDatabase.h
live inDatabase.cpp
).Try this:
Alternatively:
您引用
Database.h
中的代码,因此您必须在库中或通过对象文件Database.o
(或源文件数据库.cpp
)。You refer to code from
Database.h
so you have to provide an implementation, either in a library or via an object fileDatabase.o
(or a source fileDatabase.cpp
).您还需要编译Database.cpp,并将两者链接在一起。
这:
尝试将
main.cpp
编译为完整的可执行文件。由于Database.cpp
中的代码从未被触及,因此您会遇到链接器错误(您调用从未定义的代码)并且:
将这两个文件编译为可执行文件
最后一个选项:
首先编译这两个文件文件划分为单独的对象文件 (.o),然后将它们链接在一起形成一个可执行文件。
您可能想了解 C++ 编译过程的工作原理。
You need to compile
Database.cpp
as well, and link the two together.This:
tries to compile
main.cpp
to a complete executable. Since the code inDatabase.cpp
is never touched, you get linker errors (you call out into code that is never defined)And this:
compiles both files into an executable
The final option:
First compiles the two files to separate object fiels (.o), and then links them together into a single executable.
You may want to read up on how the compilation process in C++ works.
而不是
尝试类似的方法
,这应该修复链接过程中丢失的引用。
Instead of
try something like
This should fix missing references in the linking process.
您正在尝试在没有数据库源文件的情况下编译 main.cpp 。在 g++ 命令中包含数据库对象文件,这些函数将被解析。
我几乎可以向你保证,这将变得非常痛苦。我建议使用 make 来管理编译。
You're trying to compile main.cpp without the Database source files. Include the Database object file in the g++ command and these functions will be resolved.
I can pretty much guarantee to you that this will become a pain fast. I recommend using make to manage compilation.