tags:

views:

119

answers:

3

-I am trying to create a shared object libfoo.so. libfoo.so is created from "foo.c" - Assume that I include headers "static.h" and "Dynamic.h" where in I want the compiler to
resolve the symbols for Static.h and leave the rest ie from Dynamic.h for runtime. - How do i do this ? What are the CFLAG and LDFLAG options that I need to pass. - My makefile is setup to create a shared object using the CFLAGS=fPIC , shared , W1,export-dynamic. - In the include paths i Specify the correct location for "Static.h"

Can someone help me ?

A: 

I believe the functionality you're describing is something you essentially get for free with the GCC linker. During the linking process, the linker will resolve all symbols your code references to the libraries passed to it on the command line. If the referenced symbol name is contained within a static library (a .a file) it will be 'statically' linked and if the symbol is, instead, within a dynamic link library (a .so file) it will be dynamically linked at program execution time.

Generally speaking, you shouldn't need to care whether your symbols are statically or dynamically linked as it should have zero impact on your C/C++ code. From your description, it's difficult to understand the motivation for your question but there's a chance you may have a need for explicitly loading a dynamic link library via the dlopen() system call. If the first paragraph doesn't answer your question, could you describe the general problem you're trying to solve?

Rakis
Thanks for the reply. I am extending a snmp agent and writing a *.so object to provide the extended functionality. A summary of the makefile CFLAGS += $(INCDIRS) -shared -fPIC CFLAGS += fno-strict-aliasingLDFLAGS += export-dynamic SHLIB1 = SomeName.so SHLIB1_SRCS = foo.cand a bunch of there makefile macros that are generic to the system.
Swaroop S
I keep seeing dlopen failed: /SomeDir/lib/ifTable.so: undefined symbol: _ZN15SomeRecord4initER14TransactionRep And the source of this is a header file i included. Once I remove that particular header file I dont see this message.
Swaroop S
Typically 'undefined symbol' means you're neglecting to link against a required library. One way to figure out which .so file defines that symbol (and thus the one to link against) is to run the following command on the .so's that might contain the missing symbol: `objdump -t <suspect_library>.so | grep SomeRecord4init`
Rakis
A: 

dlopen(), dlfree(), dlsym(), dlerror() are there for you to open external runtime libraries. You have to declare function pointers to those external objects and then resolve them at runtime.

If you simply leave references "bare" in the code, the linker will atempt to resolve the symbol. It will either resolve it or throw an error, maening you do not get an executabel image. I don't know of any linker options to exclude trying to link unresolved symbols.

Or I don't get what you are trying to do.

jim mcnamara
A: 

As long as all of the dynamic symbols are accounted for in the respective headers (via externs), but potentially not declared, they can be overwritten at later points in time by the runtime linker when an application loads and defines the symbols, and the runtime linker fills in the missing bits, or barfs on the executable if one or more symbols are missing (like you have reported above with SomeRecord4init).

You can also link your application against the library (instead of using libdl, which is a pain to track down lib dependencies via many common build/analysis tools). Just using -shared and not -static in your CFLAGS will do that.

Many projects define a private and public facing APIs and fields though in C for libraries though. Maybe that's what you should do to resolve this issue?

yaneurabeya
There's also the point of weak symbols vs strong symbols, but I intentionally glossed over that point.
yaneurabeya