在linux中编译C++

Pet*_*ete 0 c++ linux compiler-errors compilation

我正在尝试在linux中编译一个简单的应用程序.我的main.cpp看起来像

#include <string>
#include <iostream>
#include "Database.h"

using namespace std;
int main()
{
    Database * db = new Database();
    commandLineInterface(*db);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

像你想象的那样,搜索这样的东西到处都是.有关我可以采取哪些措施来解决问题的建议?

Oli*_*rth 5

那些是链接器错误.这是抱怨,因为它试图以产生最终的可执行文件,但它不能,因为它有没有对象代码Database的函数(编译器不推断对应函数的定义Database.h生活Database.cpp).

试试这个:

g++ -std=c++0x main.cpp Database.cpp -o test
Run Code Online (Sandbox Code Playgroud)

或者:

g++ -std=c++0x main.cpp -c -o main.o
g++ -std=c++0x Database.cpp -c -o Database.o
g++ Database.o main.o -o test
Run Code Online (Sandbox Code Playgroud)