g++ undefined reference to `main'

1.4k views Asked by At

I have a gcc 5.2.0 configured as follows :

Using built-in specs.
COLLECT_GCC=gcc-5.2.0
COLLECT_LTO_WRAPPER=/usr/local/lvm/gcc-5.2.0/libexec/gcc/x86_64-unknown-linux-gnu/5.2.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --prefix=/usr/local/lvm/gcc-5.2.0 --enable-checking=release --with-gmp=/usr/local/lvm/gmp-6.0.0 --with-mpfr=/usr/local/lvm/mpfr-3.1.2 --with-mpc=/usr/local/lvm/mpc-1.0.3 --enable-languages=c,c++,fortran,objc,obj-c++ --with-isl=/usr/local/lvm/isl-0.14 --with-cloog=/usr/local/lvm/cloog-0.18.4 --program-suffix=-5.2.0
Thread model: posix
gcc version 5.2.0 (GCC)

and g++ :

Using built-in specs.
COLLECT_GCC=g++-5.2.0
COLLECT_LTO_WRAPPER=/usr/local/lvm/gcc-5.2.0/libexec/gcc/x86_64-unknown-linux-gnu/5.2.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --prefix=/usr/local/lvm/gcc-5.2.0 --enable-checking=release --with-gmp=/usr/local/lvm/gmp-6.0.0 --with-mpfr=/usr/local/lvm/mpfr-3.1.2 --with-mpc=/usr/local/lvm/mpc-1.0.3 --enable-languages=c,c++,fortran,objc,obj-c++ --with-isl=/usr/local/lvm/isl-0.14 --with-cloog=/usr/local/lvm/cloog-0.18.4 --program-suffix=-5.2.0
Thread model: posix
gcc version 5.2.0 (GCC)

I have the following simple c++ code in tmp2.cpp file :

extern "C" {
      double mysum(double x, double y)
      {
            return x+y;
      }
}

that I am trying to compile into a dynamic library (.so) as follows :

export LD_LIBRARY_PATH=/usr/local/lvm/gmp-6.0.0:/usr/local/lvm/mpfr-3.1.2:/usr/local/lvm/mpc-1.0.3:/usr/local/lvm/cloog-0.18.4:/usr/local/lvm/isl-0.14/lib:/usr/local/lvm/gcc-5.2.0/lib64
export PATH=/usr/local/lvm/gcc-5.2.0/bin/:$PATH
g++-5.2.0 -m32 -Wall -g -c ./tmp2.cpp
g++-5.2.0 -m32 -dynamiclib ./tmp2.o -o ./tmp2.so

and the last command gives me the following error :

/usr/lib/../lib32/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status

The detail output thx to -v can be found in a gist here.

I am quite new to gcc/g++ and don't really get what is going on. What's happened ?

1

There are 1 answers

0
Rahul Agrawal On

You should use -shared option of g++ for creating a shared object.

g++-5.2.0 -m32 -shared -dynamiclib ./tmp2.o -o ./tmp2.so