Author Topic: How do I create shared library step by step.  (Read 19749 times)

Offline str0g

  • Multiple posting newcomer
  • *
  • Posts: 20
How do I create shared library step by step.
« on: March 08, 2010, 03:05:02 pm »
Hello
os: ubuntu 9.10 x64
I would like to create shared library
I use creator to create project ProjExec which is console app(only contains main with return 0) and ProSlib which is shared library:
I configured:
ProSlib:
Project->Set programs and arguements->This prog provides main executable [checked] and I set path to [Generated binary from ProjExec]

ProjExec:
ProjExec->properties->projcet's dependecis->ProExec depends on ProSlib.

If I would like to build my ProSlib I got this output

-------------- Build: Debug in ProjSlib ---------------

Linking dynamic library: bin/Debug/libProjSlib.so
/usr/bin/ld: obj/Debug/main.o: relocation R_X86_64_32 against `__gxx_personality_v0' can not be used when making a shared object; recompile with -fPIC
obj/Debug/main.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings
 
What I did wrong or didn't do to get this compiled and working?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: How do I create shared library step by step.
« Reply #1 on: March 08, 2010, 03:35:09 pm »
I have no idea what you did wrong;
If you turn on full compiler logging and post the build log where an expert on building this library can read it. They should see the problem.

http://wiki.codeblocks.org/index.php?title=FAQ#Q:_How_do_I_troubleshoot_an_compiler_problem.3F


Tim S.

C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: How do I create shared library step by step.
« Reply #2 on: March 08, 2010, 04:06:12 pm »
Most probably you're building c++ code with the c compiler.
Remember that the file extension matters => .c files are compiled by c compiler, .cpp files by c++ compiler.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Zini

  • Multiple posting newcomer
  • *
  • Posts: 64
Re: How do I create shared library step by step.
« Reply #3 on: March 08, 2010, 04:14:40 pm »
Actually, the linker seems to tell you very clearly, what you did wrong and what you should do to fix the problem.

Quote
/usr/bin/ld: obj/Debug/main.o: relocation R_X86_64_32 against `__gxx_personality_v0' can not be used when making a shared object; recompile with -fPIC

Offline str0g

  • Multiple posting newcomer
  • *
  • Posts: 20
Re: How do I create shared library step by step.
« Reply #4 on: March 08, 2010, 04:39:13 pm »
Thanks for fast answer.

recompile with -fPIC I have not idea guide me please.

My files are cpp
That was full command line output :/
Googling gave me no solution which I know how to use...
http://code.google.com/p/gosu/issues/detail?id=38
I have g++ 4.4 with multilib

Exec main.cpp
Code
///Headers
#include <cstdlib>
#include <iostream>
#include <string>

///Specials
using namespace std;
///Globals Varuabels

int main(){
    return 0;
}

Slib main.cpp
Code
#ifdef __cplusplus
extern "C" {
#endif
    // A function adding two integers and returning the result
    int SampleAddInt(int i1, int i2)
    {
        return i1 + i2;
    }

    // A function doing nothing ;)
    void SampleFunction1()
    {
        // insert code here
    }

    // A function always returning zero
    int SampleFunction2()
    {
        // insert code here

        return 0;
    }
#ifdef __cplusplus
}
#endif
« Last Edit: March 08, 2010, 04:40:47 pm by str0g »

Offline Zini

  • Multiple posting newcomer
  • *
  • Posts: 64
Re: How do I create shared library step by step.
« Reply #5 on: March 08, 2010, 04:47:01 pm »
-fPIC is a compiler flag, that should be used when compiling the source files of the shared library. Probably goes under "Other Options". Consult your Code::Blocks manual about how to set compiler flags.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: How do I create shared library step by step.
« Reply #6 on: March 08, 2010, 06:22:54 pm »
FYI:

You really need to turn on Full Compiler Logging and ask in a group that supports the library.
Or an Linux person who builds Libraries. 

Tim S.
« Last Edit: March 08, 2010, 06:44:04 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: How do I create shared library step by step.
« Reply #7 on: March 08, 2010, 06:35:21 pm »

ProSlib:
Project->Set programs and arguements->This prog provides main executable [checked] and I set path to [Generated binary from ProjExec]

This does not sound right; but, it might be OK.
I will try it and see what happens.
I can not find the option under windows.

The code Compiles under Windows; with no errors; no idea without Good Full Compiler Build log what the issue is.

Tim S.
« Last Edit: March 08, 2010, 06:43:07 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline str0g

  • Multiple posting newcomer
  • *
  • Posts: 20
Re: How do I create shared library step by step.
« Reply #8 on: March 08, 2010, 10:31:27 pm »
Adding compilation flag fix the problem, thanks!

How to include your library?
#include "yourlibheaderFile.hpp"
build options->Search directories->Compiler->add dir with headers.
build options->linker link libraries->add dir to your library.
« Last Edit: March 09, 2010, 12:15:23 am by str0g »