How to use external / external links in C or C ++?

EDIT: I suppose I should clarify, in case that matters. I am on AIX Unix, so I use VAC compilers - no gnu compilers. End editing


I'm pretty rusty in C / C ++, so forgive me if this is a simple question.

I would like to use common functions from several of my C programs and put them in shared libraries or shared objects. If I did this in perl, I would put my submarines in the perl module and use this module if necessary.

For example, suppose I have this function:

int giveInteger()
{
    return 1034;
}

Obviously, this is not an example of the real world, but if I wanted to share this function, how could I continue?

I am sure that I have 2 options:

  • . - , .
  • ( ), . , ( ), .

?

If so, how can I use both of these methods? I searched a lot, and it seems to me that I find information on how I can link my own program to another shared library, but not how to create my own common functions and compile them so that I can use them in my own program .

Many thanks!

Brian


EDIT:

Conclusion

Thank you all for your help! I thought I would add to this post what works for me (for dynamic shared libraries on AIX) so that others can benefit:

I will compile my general functions:

xlc -c sharedFunctions.c -o sharedFunctions.o

Then make it a shared object:

xlc -qmkshrobj -qexpfile=exportlist sharedFunctions.o
xlc -G -o libsharedFunctions.so sharedFunctions.o  -bE:exportlist

Then link it to another program:

xlc -brtl -o mainProgram mainProgram.c  -L. -lsharedFunctions

, : http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/topic/com.ibm.vacpp7a.doc/proguide/ref/compile_library.htm

, !

+3
3

, . , , , , .

:

gcc -c *.c

-c , .c, . :

ar rcs libmystuff.a *.o 

man ar , rcs. libmystuff.a a rchive ( zip ), . :

gcc *.c libmystuff.a -o myprogram

. , . . .

gcc -shared -o libmystuff.so *.c

, libmystuff.so s hared o bject. , , /etc/ld.so.conf, -L GCC LD_LIBRARY_PATH. lib .so , gcc.

gcc -L. -lmystuff *.c -o myprogram

gcc GNU. , , -###: Gcc , .

( ), . Linux GCC linker.

+5

. , ++ C. , R M, g++, :

$ g++ -o myapp myapp.cpp myfunc.c giveint.c

...

$ gcc -c myfunc.c
$ gcc -c giveint.c
$ g++ -c myapp.cpp
$ g++ -o myapp myapp.o myfunc.o

; ++

extern "C" {
    int myfunc(int,int);
    int giveInterger(void);
}
+4

.

giveInteger() () , , () , , , ; [1].

; , .

++ AIX ; make++ SharedLib shell script. VAC 5.0 6.0 . , , , [2]:

xlC -G -o shr.o giveInteger.cc
xlC -o myapp main.cc shr.o

[1] Makefile ( ), make.

[2] There is a certain feature of AIX that can complicate the situation: by default, shared libraries are loaded into memory and “stuck” there until a subsequent reboot. Thus, you can restore shr.o, restart the program and watch the "old" version of the executable library. To prevent this, a common practice is to make shr.o inaccessible to the world:

chmod 0750 shr.o
0
source

All Articles