Showing posts with label gcc. Show all posts
Showing posts with label gcc. Show all posts

Monday, December 19, 2011

How to call C functions from CPP file

Reference: How to call c function from CPP file

For example

filea.c
int apb(int a, int b)
{
return a+b;
}
filea.h
int apb(int a, int b);
fileb.cpp
#include "filea.h"
int main(void)
{
 return apb(5,1);
}
compiler tells me error:
Error: L6218E: Undefined symbol apb(int, int) (referred from fileb.o).
Solution:
extern "C"
{
#include "filea.h"
}

int main(void)
{
 return apb(5,1);
}

Sunday, December 04, 2011

How to compile library and link?


1) gcc -c hello.c
Compile hello.c to object file.
2) ar rcs libhello.a hello.o
(or ar ruv hello.a hello.o)
Link object to static library.
3) gcc test.c -o test libhello.a
Compile executable file and link libhello.a
To create the shared (*.so) library, use these steps
1) gcc -fpic -c hello.c
2) gcc -shared -o libhello.so hello.o
3) gcc test.c -o test -L. -lhello
ar x /usr/lib/libx86.a
extract object file from libx86.a
ar t /usr/lib/libx86.a
Display a table listing the contents of archive…
ar – create, modify, and extract from archives
t Display a table listing the contents of archive, or those of the files listed in member… that are present in the archive. Normally only the member name is shown; if you also want to see the modes (permissions), timestamp, owner, group, and size, you can request that by also specifying the v modifier.
If you do not specify a member, all files in the archive are listed. If there is more than one file with the same name (say, fie) in an archive (say b.a), ar t b.a fie lists only the first instance; to see them all, you must ask for a complete listing—in our example, ar t b.a.
x Extract members (named member) from the archive. You can use the v modifier with this operation, to request that ar list each name as it extracts it.
If you do not specify a member, all files in the archive are extracted. Files cannot be extracted from a thin archive.
nm – list symbols from object files