在linux中编译C ++ [英] Compile C++ in linux

查看:134
本文介绍了在linux中编译C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

其中Database.h是我的头,并有一个相应的Database.cpp。编译时得到以下错误:

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?

推荐答案

错误。它是抱怨,因为它试图产生最终的可执行文件,但它不能,因为它没有对象代码数据库函数(编译器不推断函数定义对应到 Database.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 to Database.h live in Database.cpp).

尝试此操作:

g++ -std=c++0x main.cpp Database.cpp -o test

或者:

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

这篇关于在linux中编译C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆