Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

link shared library with math function in C

Posted on 2014-09-07
9
Medium Priority
?
227 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 46

Accepted Solution

by:
Kent Olsen earned 500 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 500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 46

Expert Comment

by:Kent Olsen
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
 

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 46

Expert Comment

by:Kent Olsen
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

824 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