c++ link error: undefined reference to `main'

c++ link error: undefined reference to `main'

Mark Suhovecky's picture

I'm using intel 12.0 on RHEL 5.8

I'm trying to build a shared libary that's a mix of C and C++ files. I can't get it to link. Here's the build, and the errors:

icc
-std=c99 -I/opt/crc/R/R-2.15.0-intel/inst/lib64/R/include -DNDEBUG
-I/usr/local/include -fpic -g -O2 -std=c99 -c csemnlm.c -o
csemnlm.o

icc -std=c99
-I/opt/crc/R/R-2.15.0-intel/inst/lib64/R/include -DNDEBUG
-I/usr/local/include -fpic -g -O2 -std=c99 -c uncmin.c -o uncmin.o

icpc -I/opt/crc/R/R-2.15.0-intel/inst/lib64/R/include -DNDEBUG -I/usr/local/include -fpic -g -O2 -c csem.cpp -o csem.o

icpc
-L/usr/local/lib64 -o sem.so csemnlm.o uncmin.o csem.o -g -O2 -lm
-L/opt/crc/R/R-2.15.0-intel/inst/lib64/R/lib -lRlapack
-L/opt/crc/R/R-2.15.0-intel/inst/lib64/R/lib -lRblas -lifport -lifcore
-limf -lsvml -lm -lipgo -lirc -lpthread -lirc_s -ldl
-L/opt/crc/R/R-2.15.0-intel/inst/lib64/R/lib -lR
/afs/crc.nd.edu/x86_64_linux/intel/12.0/composerxe-2011.2.137/compiler/lib/intel64/libimf.so:
warning: warning: feupdateenv is not implemented and will always fail
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
make: *** [sem.so] Error 1

Since
the error comes trying to link a to gcc library on the system, my first
guess is that cpc is having troubles doing this ( I've see old postings
to this effect.)

My Second guess would be that I can't us cpc to link c and C++

Any ideas appreciated,

Mark

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Judith Ward (Intel)'s picture

If you're trying to create a shared library don't you need to use the options -shared -Wl,soname,libsem.so instead of -o sem.so?

Judy

sukruth-v (Intel)'s picture

Hi Mark,
This is the way that you can create and use shared libs(.so):-

1. Compile the source file(.c or .cpp) example :- icpc -c -fPIC func1.cpp

//func1.cpp

#include

#include "func1.h"

using namespace std;

int call_func1()

{

cout<<"static func1 was called";

return 12;

}

//func1.h
int call_func1();

Then take the object file created (func1.o) and use it in the prescribed way:-

//main1.c

#include

#include "func1.h"

int main()

{

call_func1();

return 0;

}
icpc func1.o -shared -o libshare.so
icpc main1.c ./libshare.so //OK
icpc main1.c //main1.c:(.text+0x29): undefined reference to `call_func1()'

This is the way we generally create and use shared libraries. If you have any further doubts please do let me know and i would be happy to help you.

OS used ubuntu 10.04 LTS.

Thanks & Regards,
Sukruth H.V

Login to leave a comment.