Pete
Pete

Reputation: 10871

Compile C++ in linux

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?

Upvotes: 0

Views: 1825

Answers (5)

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247969

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.

Upvotes: 1

Zach Rattner
Zach Rattner

Reputation: 21353

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.

Upvotes: 0

jbvo
jbvo

Reputation: 141

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.

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272497

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

Upvotes: 5

Dirk is no longer here
Dirk is no longer here

Reputation: 368231

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

Upvotes: 2

Related Questions