Compile C++ in linux

戏子无情 提交于 2019-12-13 05:26:09

问题


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?


回答1:


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).

Try this:

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

Alternatively:

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



回答2:


You refer to code from Database.h so you have to provide an implementation, either in a library or via an object file Database.o (or a source file Database.cpp).




回答3:


You need to compile Database.cpp as well, and link the two together.

This:

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

tries to compile main.cpp to a complete executable. Since the code in Database.cpp is never touched, you get linker errors (you call out into code that is never defined)

And this:

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

compiles both files into an executable

The final option:

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

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.




回答4:


Instead of

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

try something like

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

This should fix missing references in the linking process.




回答5:


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.



来源:https://stackoverflow.com/questions/5745903/compile-c-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!