C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 26th, 2003, 10:11 AM
imchi imchi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 129 imchi User rank is Sergeant (500 - 2000 Reputation Level)imchi User rank is Sergeant (500 - 2000 Reputation Level)imchi User rank is Sergeant (500 - 2000 Reputation Level)imchi User rank is Sergeant (500 - 2000 Reputation Level)imchi User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 20 h 35 m 8 sec
Reputation Power: 20
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.

Reply With Quote
  #2  
Old June 26th, 2003, 10:13 AM
imchi imchi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 129 imchi User rank is Sergeant (500 - 2000 Reputation Level)imchi User rank is Sergeant (500 - 2000 Reputation Level)imchi User rank is Sergeant (500 - 2000 Reputation Level)imchi User rank is Sergeant (500 - 2000 Reputation Level)imchi User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 20 h 35 m 8 sec
Reputation Power: 20
correction: "make executable", not "make test" is the command in question.

Reply With Quote
  #3  
Old June 26th, 2003, 11:51 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,824 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 1 h 9 m 26 sec
Reputation Power: 446
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):
Quote:
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):
Quote:
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 12:03 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > problems building an executable with g++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway