0

I am building my C Program using GCC on AIX. While building it I am referring to some libraries those are complied and build using XLC on AIX. My library location has two forms i.e same library is present in the form of *.so and *.a. But when while building my application it statically links to *.a library instead of *.so And while loading the library dynamically it refers to *.so is there any way to control this so that my static and dynamic linking of library only happens with *.a.

P.S I can't remove the *.so libs from library location.

Commands I am using are as follows:

$(PRODUCT)/lib contains libraries(*.a, *.so) those are build using XLC on AIX

CC=/usr/bin/gcc -maix64

DynamicInboxLibGcc:

$(CC) -o InboxLib.o -I$(PRODUCT)/include -I. -L$(PRODUCT)/lib -fPIC -c InboxLib.c $(LIBS)   
$(CC) InboxLib.o -I$(PRODUCT)/include -ar -shared -o $(PRODUCT)/libInboxLibXlc.a -L$(PRODUCT)/lib $(LIBS)

WrapperLibGcc:

$(CC) -o DynamicLinkSample.o -I$(PRODUCT)/include -I. -L$(PRODUCT)/lib -qPIC -c DynamicLinkSample.c $(LIBS)
$(CC) DynamicLinkSample.o -I$(PRODUCT)/include -ar -shared -o $(PRODUCT)/CustomLib.a -L$(PRODUCT)/lib $(LIBS)

DynamicMainProgramGcc:

$(CC) -o DynamicMainProgram -wl -brtl MainProgram.c -Wall -fPIC -I$(PRODUCT)/include -I. -L/usr/lib/threadsi -L$(PRODUCT)/lib $(LIB_PATH)/CustomLib.a $(LIB_PATH)/libInboxLibXlc.a -ldl $(LIBS)
diago
  • 33
  • 1
  • 1
  • 7
  • Please give the exact commands you are using to build your program, you question is unclear otherwise. – Basile Starynkevitch Aug 20 '14 at 06:55
  • 1
    You can't load a static library (*.a) dynamically. If you still have a problem, also show us the command line you use to link. – nos Aug 20 '14 at 06:56
  • I do not know how familiar with AIX you are. So you may find this comment off-topic. But anyway: AIX - on contrary from other Unixes - does not use executable format .ELF but uses it's own format called XCOFF. This format also supports static and dynamic libraries, but the meaning of static vs. is slightly different. For example all the PowerPC code is position independent by "nature" and this makes people think that there is no difference between static and dynamic lib on AIX. XCOFF format also supports 32bit and 64bit symbols in a single file. – ibre5041 Aug 20 '14 at 07:38
  • Are you using CMAKE or Automake tools? You are building "shared" library but you want it to have suffix .a? DynamicInboxLibGc: ~-shared -o $(PRODUCT)/libInboxLibXlc.a~. I recall there were some problems with GNU Linker(ld). When using gcc on AIX it's better to use OS provided linker. Try to split the steps and try to call steps separately. – ibre5041 Aug 20 '14 at 09:39
  • no I am not using CMAKE, I am using make. My point of concern is not the shared library which I am building it. The libraries which I am referring are present at $(PRODUCT)/lib. Here I have same library with *.a and *.so which is causing the problem – diago Aug 20 '14 at 10:45

2 Answers2

0
  • try to execute truss on AIX and check how exactly is linker executed from gcc (and who's version IBM's or GNU's)
  • try to execute ld manually to link the binary
  • try to split the variable $(LIBS) into static ones and dynamic ones. They is which parameters are passed from gcc to linker.
  • If you were using gnu ld then you might use something like this gcc <objectfiles> -Wl,-Bstatic -lstatic1 -lstatic2 -Wl,-Bdynamic -ldynamic1 -ldynamic2. Option -Wl means (pass the following parameter to the linker). -Bstatic, -BDynamic are parameters of GNU linker. If you do not use them gcc might deduce it's own parameters for linker. But maybe it fails somehow. Especially when IBM's linker uses different parameters.

PS: I would recommend AIX ld before GNU one.

PS1: -fPIC is ignored on AIX

ibre5041
  • 4,903
  • 1
  • 20
  • 35
  • problem is the libraries I am referring to are third party libraries their name can change in future that is why I don't want to link them directly by specifying their name. Is there any way that I can force linker to link only *.a libraries? – diago Aug 21 '14 at 10:33
  • Most probably yes. But first of all you must know whether you are using `ld` from GNU's binutils or `ld` provided by IBM(AIX). When you execute gcc it will ONLY compile the sources. It will not perform any linking. Instead of that it will execute `ld` silently on background. You must know which `ld` was executed and which parameters were passed to it. Then you check documentation of that particular linker. Many parameters of both linkers are the same, but when comes to details you must know which linker are you using. – ibre5041 Aug 21 '14 at 11:31
0

GNU Binutils does not function correctly on AIX. One must use AIX assembler and linker.

AIX linking prefers files with .a extension over .so file extension by default.

AIX has a different naming scheme than SVR4/Linux. Normally AIX shared objects are members of archive libraries, e.g., libfoo.a(shr.o) meaning the shared object (traditionally called shr.o) is an archive member of libfoo.a. One normally does not have static archives, or at least separate archives of static objects. Yes, this conflicts with the default manner that most software packages are built and installed.

One can prefer .so file extension with the -brtl linker option, but this will create other issues because it prepares the application for runtime linking, meaning overriding symbols at runtime. This feature is very expensive at runtime.

It is better to not have libraries with the same name that have both .a and .so file extension versions available. If one wants to link dynamically, only provide the dynamic version of the library (either .so or .a) in the library search path directories.