Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » How to create class from shared library project?(Create class in shared library in eclipse)
How to create class from shared library project? [message #1778086] Sat, 09 December 2017 12:43 Go to next message
CR Lin is currently offline CR LinFriend
Messages: 1
Registered: November 2017
Junior Member
I created class from shared library from CDT shared library project (Oxygen 4.7)

but I got error when compiling , and the console showed the error as below

Quote:
19:59:34 **** Incremental Build of configuration Debug for project MyShareLib ****
make all
Building target: libMyShareLib.so
Invoking: Cross G++ Linker
g++ -shared -o "libMyShareLib.so" ./ext.o
/bin/ld: ./ext.o: relocation R_X86_64_32S against `_ZTV3ext' can not be used when making a shared object; recompile with -fPIC
./ext.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [libMyShareLib.so] Error 1



How to create class in shared library project?

here are my source code and attached it as well

================ext.h===============
/*
* ext.h
*
* Created on: Dec 9, 2017
* Author: linux
*/

#ifndef EXT_H_
#define EXT_H_

#include<iostream>
using namespace std;

class ext {
public:
ext();
virtual ~ext();
void ext_printf(void)
{
cout<<"From ext"<<endl;
return;
}
};

#endif /* EXT_H_ */

================ext.cpp======================
/*
* ext.cpp
*
* Created on: Dec 9, 2017
* Author: linux
*/

#include "ext.h"

ext::ext() {
// TODO Auto-generated constructor stub

}

ext::~ext() {
// TODO Auto-generated destructor stub
}

===============================================
  • Attachment: MySO.tar.gz
    (Size: 37.20KB, Downloaded 16 times)
Re: How to create class from shared library project? [message #1778212 is a reply to message #1778086] Tue, 12 December 2017 01:59 Go to previous message
David VavraFriend
Messages: 733
Registered: October 2012
Senior Member
This isn't a CDT problem so it's OT for this forum.

Still,
'class' is a compile time construct.
You are getting a linker error.
Error was: /bin/ld: ./ext.o: relocation R_X86_64_32S against `_ZTV3ext' can not be used when making a shared object; recompile with -fPIC
How did you compile ext.cpp?
Did you recompile it with -fPIC?

GNU documentation of -fpic and -fPIC
Latest command line options (GCC-7.2): https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/#toc-GCC-Command-Options
and: https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Code-Gen-Options.html#Code-Gen-Options
Quote:

-fpic
Generate position-independent code (PIC) suitable for use in a shared library, if supported for the target machine. Such code accesses all constant addresses through a global offset table (GOT). The dynamic loader resolves the GOT entries when the program starts (the dynamic loader is not part of GCC; it is part of the operating system). If the GOT size for the linked executable exceeds a machine-specific maximum size, you get an error message from the linker indicating that -fpic does not work; in that case, recompile with -fPIC instead. (These maximums are 8k on the SPARC, 28k on AArch64 and 32k on the m68k and RS/6000. The x86 has no such limit.)

Position-independent code requires special support, and therefore works only on certain machines. For the x86, GCC supports PIC for System V but not for the Sun 386i. Code generated for the IBM RS/6000 is always position-independent.

When this flag is set, the macros __pic__ and __PIC__ are defined to 1.

-fPIC
If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table. This option makes a difference on AArch64, m68k, PowerPC and SPARC.

Position-independent code requires special support, and therefore works only on certain machines.

When this flag is set, the macros __pic__ and __PIC__ are defined to 2.

General info for building .so files: http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

EDIT: originally quoted from GCC-3.0. Hasn't changed much.

[Updated on: Tue, 12 December 2017 02:11]

Report message to a moderator

Previous Topic:MASTER??
Next Topic:Change intermediate files extension
Goto Forum:
  


Current Time: Wed Jun 20 17:29:32 GMT 2018

Powered by FUDForum. Page generated in 0.01728 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top