Solved

link shared library with math function in C

Posted on 2014-09-07
9
214 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:
Kent Olsen 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

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 45

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference between mmap and malloc/valloc + mmap fixed 18 276
C++ vs C compilers 13 164
Windows Server 2012  Backup - HyperV host 18 91
windows 10 pro lost profile. 10 68
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

749 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