Compiling Shared Libraries

I've just finished writing some C++ classes. I've written some test cases to test them and everything seems to be working fine, so I would like to compile the classes into a shared library (.so) so they can be shared. However, when I try to compile, I get the following error.

1
2
3
4
5
6
Undefined symbols:
  "_main", referenced from:
      start in crt1.10.5.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [lib] Error 1


This is the command that generated this error message.

g++ -Wall -shared -o lib/libcppbio.so obj/FastaIO.o obj/FastaSequence.o

The .o files were created with these commands without problem.

1
2
g++ -Wall -c -o obj/FastaIO.o -I inc src/FastaIO.cpp
g++ -Wall -c -o obj/FastaSequence.o -I inc src/FastaSequence.cpp


I compiled my test cases successfully with this command.

g++ -Wall -o bin/test obj/test.o obj/FastaIO.o obj/FastaSequence.o

Do you have any idea what could be causing this?
I am working on Mac OS X. I thought I would try it on a Linux system (Fedora Core). I ran the same commands and the compiler told me to recompile with -fPIC flags. I did so and it was successful. However, I did the same thing back on my Mac and it still gives me the same errors. What could be causing this?

These commands were successful on Linux but not on Mac OS X.
1
2
3
g++ -Wall -c -fPIC -o obj/FastaIO.o -I inc src/FastaIO.cpp
g++ -Wall -c -fPIC -o obj/FastaSequence.o -I inc src/FastaSequence.cpp
g++ -Wall -I inc -shared -o lib/libcppbio.so obj/FastaIO.o obj/FastaSequence.o
I may be wrong, since I've never made a shared library, but aren't you not supposed to have a main function in them?
Neither of the two classes contain a main method. Another reason why the error is so confusing.
OK, try compiling like this instead:
g++ -Wall -dynamiclib -flat_namespace -Iinc <source files> -o <output dynamic library>.so

This seemed to work on Mac. For whatever reason, compiling libraries on Mac and on Linux seem to be very different, even if the compiler itself is the same.
Last edited on
Thanks, JCED! I appreciate it.

I'll admit I'm no guru when it comes to C++, less so with C. Do you know of any references for gcc/g++ and their differences between OS X and Linux distros?
Topic archived. No new replies allowed.