#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    179
    Rep Power
    28

    problems building an executable with g++


    Hello,

    I'm trying to compile by C++ code into a shared object and
    then create an executable that would use functions from
    that shared object.
    The SO build works fine.
    The compilation of the executable is what is giving me problems. My makefile looks like this:

    executable: so
    $(CXX) $(DEFINES) $(INCLUDES) -L$(LIB_DIR) \
    $(SRC_DIR)/executable.cpp \
    -o $(BIN_DIR)/executable
    so:
    $(CXX) $(DEFINES) $(INCLUDES) -L$(LIB_DIR) -G \
    $(SRC_DIR)/module1.cpp \
    $(SRC_DIR)/module2.cpp \
    ... etc
    -o $(LIB_DIR)/myobject.so

    "make so" works fine, it generates the file myobject.so in the directory $(LIB_DIR). But when I do "make test", I get an error
    like "Undefined symbol..first referenced in the file..". But that symbol was defined in one of the modules. I would think that it would be now available from the myobject.so file which is used by the task "executable". Why wouldn't it see the symbol?
    Anybody have any idea?
    Thanks in advance.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    179
    Rep Power
    28
    correction: "make executable", not "make test" is the command in question.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    My book's not clear on that ("Beginning Linux Programming" 2nd ed., Stones and Matthew), but it looks like you need to name the shared library in the call to gcc:

    executable: so
    $(CXX) $(DEFINES) $(INCLUDES) -L$(LIB_DIR) -lmyobject.so \
    $(SRC_DIR)/executable.cpp \
    -o $(BIN_DIR)/executable

    What I'm not clear on is whether you need to give the .so extension or not. The books gives an example that links in library foo and says (pg 21):
    The -lfoo option tells the compiler to use a library called libfoo.a (or a shared library, libfoo.so if one is present).
    EDIT:
    Your make so looked like you were creating an executable instead of a library, so I checked my notes from my other book (at home -- "Linux Programming by Example" by Kurt Wall):
    Building a Shared Library (pg 309):
    1. compile with gcc's -fPIC option
    2. don't use gcc's -fomit-frame-pointer option
    3. use gcc's -shared and -soname options
    4. use gcc's -Wl option to pass arguments to the linker, ld
    5. explicitly link against the C library by using gcc's -l option.
    Eg,
    build the object file
    gcc -fPIC -g -c liberr.c -o liberr.o
    link the library
    gcc -g -shared -Wl,-soname,liberr.so -o liberr.so.1.0.0 liberr.o -lc
    create the symbolic link from the full library name to the soname
    ln -s liberr.so.1.0.0 liberr.so.1
    create the symbolic link from the full library name to the name of the shared library
    ln -s liberr.so.1.0.0 liberr.so

    Using a Shared Library (pg 310):
    compile with the -l and -L options and use $LD_LIBRARY_PATH
    gcc -g errtest.c -o errtest -L. -lerr
    LD_LIBRARY_PATH=$(pwd) ./errtest
    Last edited by dwise1_aol; June 26th, 2003 at 01:03 PM.

IMN logo majestic logo threadwatch logo seochat tools logo