Solved

link shared library with math function in C

Posted on 2014-09-07
9
208 Views
Last Modified: 2014-09-22
link shared library with math.h functions in C
I need command line example.

Generally for this:
http://chimera.labs.oreilly.com/books/1230000000393/ch15.html
0
Comment
Question by:Nusrat Nuriyev
  • 3
  • 3
  • 3
9 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 250 total points
ID: 40308715
Hi Nusrat,

It depends mostly on the tools that you're using.  The O/S, compiler, and linker.

If you're using GCC, it a command line option.

  gcc -l library ...

Just add "-l library" to the command line with the other options.  GCC will pass that to the linker.


Kent
0
 
LVL 5

Assisted Solution

by:Sapa
Sapa earned 250 total points
ID: 40308815
Try to use:

gcc -shared -o myfile.so myfile.c -lm

where  myfile,c is your source file, but myfie.so is the shared library you want to create
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40335125
0
 
LVL 45

Expert Comment

by:Kdo
ID: 40335382
Hi Nusrat,

If you're trying to link a library to your compiled code, you don't need the -fPIC parameter.  -fPIC indicates that the code that you're compiling is to be inserted into a library for later use.  Note that -fPIC isn't needed for most C functions that are going to be placed in a library.


Kent
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Nusrat Nuriyev
ID: 40335456
[root@localhost tmp]# gcc -shared -o libsample.so sample.c -lm
/bin/ld: /tmp/ccjQThFb.o: relocation R_X86_64_PC32 against undefined symbol `hypot@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status

Could you explain what does this mean? And where to read about this?
0
 
LVL 5

Expert Comment

by:Sapa
ID: 40335901
Just add -fPIC as suggested. Like:

gcc -shared -fPIC -o libsample.so sample.c -lm
0
 
LVL 5

Expert Comment

by:Sapa
ID: 40335905
PIC means Position Independent Code. This code has neither conditional nor unconditional jumps to fixed addresses and can be loaded (actually mapped) from arbitrary address. It is important for shared libraries because nobody knows during library linking the address where they be mapped to during programm execution.
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40336035
Yes, I should add that, the question was what does this mean:
relocation R_X86_64_PC32 against undefined symbol `hypot@@GLIBC_2.2.5' can not be used when making a shared object;
0
 
LVL 45

Expert Comment

by:Kdo
ID: 40336554
Hi Nusrat,

The issue came about as systems expanded from 32 to 64 bits.  The high-level explanation is that the object in question isn't compatible with the module being created and is therefore considered undefined.  It's kind of like a C++ method void F1(int X) being undefined when the compiled program calls F1 ("string).

A 64-bit program linking to or via a 32-bit object could be disastrous as the other 32 bits are the 32-bits next to the stored object.  A read using the object is undefined.  A write of the object clobbers the object next to it.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now