views:

44

answers:

3

Hi everyone,

I am having trouble compiling a program I have written. I have two different files with the same includes but only one generates the following error when compiled with g++

/usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start':
/build/buildd/eglibc-2.10.1/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main'
collect2: ld returned 1 exit status

The files I am including in my header are as follows:

#include <google/sparse_hash_map>
using google::sparse_hash_map;

#include <ext/hash_map>
#include <math.h>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>

using std::priority_queue;
using std::stack;
using std::vector;

using __gnu_cxx::hash_map;
using __gnu_cxx::hash; 

using namespace std;

Searching the internet for those two lines hasn't resulted in anything to help me. I would be very grateful for any advice. Thank you

+1  A: 

You need a main function and you don't have one. If you do have a main function, show more code please.

zneak
A: 

It looks like main is not defined. Do you have one defined for your second program? Can you post more details about the source body that fails to link?

fbrereto
Thanks. That was all I needed to figure out why my code wasn't working. Needed to define a main just to call my function.
+3  A: 

To build two separate programs you need both source files to define main() function.

To build a single program out of two source files - first compile each file with -c options (compile only) - you will get two .o files, then link these files together. Something like this:

$ g++ -Wall -pedantic -ggdb -O -c -o module0.o module0.cpp
$ g++ -Wall -pedantic -ggdb -O -c -o module1.o module1.cpp
$ g++ -Wall -pedantic -ggdb -O -o prog module0.o module1.o

to build binary prog from two source files.

If you need to link with some library, you'll have to point compiler to it's headers with -I and to objects with -L flags, then tell the linker to actually reference the library with -l.

Hope this helps.

Nikolai N Fetissov