Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In function `_start': (.text+0x20): undefined reference to `main' collect2: ld returned 1 exit status

Tags:

c++

g++

I'm new to g++ and compiling my C++ code in LINUX. I'm trying to connect several .o files as .lib using g++.

This is the command that I used "g++ -o ../fuzzy/fuzzy.lib example1.o example2.o" and got this error. Even though I try to connect a single object file and make a .lib, it doesn't work.

Your help is much appreciated.

Thanks

like image 922
parth patel Avatar asked Jan 26 '26 13:01

parth patel


2 Answers

If you are trying to build a library, the options depend on whether you want to build a static or a shared library. For example,

# shared library
g++ -shared -o name.so obj1.o obj2.o obj3.o

A static library is essentially an archive, which you can build from the .o files using the ar command.

ar <options> mylib.a obj1.o obj2.o obj3.o

If you are trying to compile an object file, you need to pass the -c option:

g++ -c ....

Otherwise, g++ attempts to compile an executable, and this requires a main function.

like image 194
juanchopanza Avatar answered Jan 29 '26 03:01

juanchopanza


To make a static library, use ar:

ar rcs mylibrary.a a.o b.o c.o

To make a shared library, use gcc to link:

gcc -o mylibrary.so -shared a.o b.o. c.o
like image 23
Kerrek SB Avatar answered Jan 29 '26 05:01

Kerrek SB