Compiling A Shared library

This is a discussion on Compiling A Shared library within the C Programming forums, part of the General Programming Boards category; I have a library that I've been writing for the past couple years, and I finally decided to port it ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    5

    Question Compiling A Shared library

    I have a library that I've been writing for the past couple years, and I finally decided to port it to C from C++. After doing all this and making sure the syntax was right in all the code, I now have come across an error I can't seem to figure out. When I try to compile the library without a main function, it tells me that main is undefined and fails. But, if I put a main function in, it compiles and builds. But attempting to link something to it will result in a redefinition of main error. I can't seem to do either of the two, and am stumped. I am using GCC in Linux, if it makes a difference.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    If this is a library, so you shouldn't need a main function. Sounds like a problem with your Makefile. You should not be attempting to link this into an application (which requires a main function for an entry point).

    What does your Makefile look like?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    5
    I use no makefile to do this; rather, just source and compiler. The commands I do use are:

    gcc -fPIC -c libectara.c
    gcc -O3 -g3 -shared -lm -ldl -Wall -Wl,-soname,libectara.so.1 -o libectara.so.1.1 *.o

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    5
    Congrats on 3k posts, by the way.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by Ectara View Post
    I use no makefile to do this; rather, just source and compiler. The commands I do use are:

    gcc -fPIC -c libectara.c
    gcc -O3 -g3 -shared -lm -ldl -Wall -Wl,-soname,libectara.so.1 -o libectara.so.1.1 *.o
    I thought the error occurred when you where compiling the library as C++ code. Do you get the same error with your original C code as well?

    The link command you have there shouldn't require a main function.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    5
    I rewrote the library in C89, so no part of it is C++ anymore. There is a second library that I wrote that depends on this library, and it happens to be written in C++, but I'm pretty sure it doesn't matter what uses it after it's compiled. More specifically, I can compile the C code, and it gives no errors, and produces a shared object, but then the C++ project that depends on it complains that there is no main function. If there is a main added to the C++ library, it compiles and produces a shared object, but linking a test program against that will result in a redefinition of main, which makes sense. What doesn't make sense, is why the C++ library demands there be a main function.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    5
    Turns out I'm an idiot, and made a typo:

    g++ -fPIC -c libectarica.cpp
    g++ -O2 -g3-shared -lm -ldl -lectara -lSDL -lGL -lGLU -Wall -Wl,-soname,libectarica.so.0 -o libectarica.so.0.8 *.o

    Instead of

    g++ -fPIC -c libectarica.cpp
    g++ -O2 -g3 -shared -lm -ldl -lectara -lSDL -lGL -lGLU -Wall -Wl,-soname,libectarica.so.0 -o libectarica.so.0.8 *.o


    Thanks for your help, anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  2. Replies: 7
    Last Post: 02-06-2009, 11:27 AM
  3. wxWidgets loading shared library problem
    By BobS0327 in forum Linux Programming
    Replies: 1
    Last Post: 05-10-2006, 07:23 PM
  4. Compiling Library Into Program.
    By Shogun in forum Linux Programming
    Replies: 10
    Last Post: 11-30-2004, 12:29 PM
  5. better c string functions
    By samps005 in forum C Programming
    Replies: 8
    Last Post: 11-04-2003, 12:28 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21