How do I source/link external functions in C or C++?

前端 未结 3 439
天涯浪人
天涯浪人 2021-02-04 21:57

EDIT: I suppose I should clarify, in case it matters. I am on a AIX Unix box, so I am using VAC compilers - no gnu compilers. End edit

<
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-04 22:11

    You need to distinguish between recompiling and relinking.

    If you put giveInteger() into a separate (archive) library, and then modify it later, you'll (obviously) need to recompile the source file in which it is defined, and relink all programs that use it; but you will not need to recompile such programs [1].

    For a shared library, you'll need to recompile and relink the library; but you will not have to relink or recompile any of the programs which use it.

    Building C++ shared libraries on AIX used to be complicated; you needed to use makeC++SharedLib shell script. But with VAC 5.0 and 6.0 it became quite easy. I believe all you need to do is [2]:

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

    [1] If you write correct Makefile (which is recommended practice), all of this will happen automatically when you type make.

    [2] There is a certain feature of AIX which may complicate matters: by default shared libraries are loaded into memory, and "stick" there until subsequent reboot. So you may rebuild the shr.o, rerun the program, and observe "old" version of the library being executed. To prevent this, a common practice is to make shr.o world-unreadable:

    chmod 0750 shr.o
    

提交回复
热议问题