objective-cgnu-makegnustep

Categories in static lib not regocnized at runtime


I am building an executable ("tool") on Linux. Using include $(GNUSTEP_MAKEFILES)/tool.make.

It's linked to a static lib that has also be build with GNUstep. The lib
contains Categories.

The executable builds fine but has errors at runtime not recognizing
methods defined in the static lib's Category:

Uncaught exception NSInvalidArgumentException, reason:  
ClassNameOfClassTheCategoryExtends(instance) does not recognize  
nameOfMethodInCategory

I am trying to fix that by passing -ObjC to the linker flags (also
tried -all_load) in the executable's GNUmakefile:

ADDITIONAL_LDFLAGS = -ObjC -all_load

But that seems to be ignored by clang. Here is the relevant output of
make install messages=yes debug=yes

clang: warning: argument unused during compilation: '-ObjC'  
[-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-all_load'  
[-Wunused-command-line-argument]

It looks like ADDITIONAL_LDFLAGS are used compiling, not linking.

Using this leads to the same result:

LDFLAGS := $(LDFLAGS) -ObjC

The excecutables GNUmakefileincludes the following:

include $(GNUSTEP_MAKEFILES)/common.make
# My make 
include $(GNUSTEP_MAKEFILES)/tool.make

The resulting command line output is:

$ make install messages=yes debug=yes
This is gnustep-make 2.9.0. Type 'gmake print-gnustep-make-help' for help.
Running in gnustep-make version 2 strict mode.
Making all for tool NameOfExcecutable...
clang -ObjC     -fuse-ld=/usr/bin/ld.gold  -pthread -fexceptions -rdynamic -fobjc-runtime=gnustep-2.0 -fblocks  -o obj/NameOfExcecutable \
./obj/NameOfExcecutable.obj/main.m.o ./obj/NameOfExcecutable.obj/MyClass.m.o ./obj/NameOfExcecutable.obj/StreamRunLoop.m.o ./obj/NameOfExcecutable.obj/Connector.m.o ./obj/NameOfExcecutable.obj/HTTPClient.m.o \
     -L/home/user/GNUstep/Library/Libraries -L/usr/GNUstep/Local/Library/Libraries -L/usr/GNUstep/System/Library/Libraries     -lgnustep-base -ldispatch -l/path/to/libOwnLib1.a -l/path/to/libOwnLib2.a -l/path/to/libOwnHavingTheCategories.a -l/path/to/libOwnLib4.a -l/path/to/libOwnLib5.a -luuid -lz -lpthread -ldl   -lpthread -lobjc   -lm

clang: warning: argument unused during compilation: '-ObjC' [-Wunused-command-line-argument]

Question:

What am I doing wrong

or

How can I work around the issue?


Solution

  • After digging into the issue of the linker not knowing the -ObjC flag (which we are used to use in Xcode) it looks like:

    To workaround we first stopped using GNUstep makefiles to
    disable all GNUstep magic understand what is going on and wrote our own makefiles.

    The actual fix to force link/load all .o files was to explicitly pass --whole-archive to the linker:

    -Wl,-whole-archive path/to/static/lib/containing/categories/libOwnLib1.a -Wl,-no-whole-archive