-lsocket issues on Linux (RedHat 9)

Place to discuss Fedora and/or Red Hat

-lsocket issues on Linux (RedHat 9)

Postby ZiaTioN » Fri Oct 08, 2004 1:19 am

I am writing this socket program in C and have run into an issue in my make file while using the -lsocket during compilation. I have to have this program compile on both Solaris and Linux. From what I have read -lsocket is not needed to be invoked during compilation on Linux but it does on Solaris? Is this true?

If this is true is there a way to determine in the make file what OS I am on and use -lsocket if on Solaris and not use it if on Linux? I know in perl I could simply compare the default variable $^O and since perl is built on C I would assume there is a way to perform this in native C. If so how could I do this?
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Postby Void Main » Fri Oct 08, 2004 7:25 am

Yes, you could implement a check in your makefile using the "uname" system command:

Code: Select all
UNAME=$(shell uname)
LIBS=
ifeq ($(UNAME),SunOS)
LIBS+=-lsocket
endif
showme:
        @echo os=$(UNAME)
        @echo libs=$(LIBS)


NOTE: I think this will only work if you are using the GNU make on both Linux and Solaris. Is that what you are using?
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby ZiaTioN » Fri Oct 08, 2004 10:48 am

Here is the make file in question:

CC= g++
CFLAGS= -g

# The -lsocket -lnsl are needed for the sockets.
# The -L/usr/ucblib -lucb gives location for the Berkeley library needed for
# the bcopy, bzero, and bcmp. The -R/usr/ucblib tells where to load
# the runtime library.


LIBS= -lnsl

all: udp_send udp_recv

udp_send: udp_send.c networks.h
$(CC) $(CFLAGS) -o udp_send udp_send.c $(LIBS)

udp_recv: udp_recv.c networks.h
$(CC) $(CFLAGS) -o udp_recv udp_recv.c $(LIBS)

clean:
rm udp_recv udp_send


I will try your suggestion though, thanx!
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Postby Void Main » Fri Oct 08, 2004 11:21 am

Yes, this should work:

Code: Select all
CC= g++
CFLAGS= -g
 
# The -lsocket -lnsl are needed for the sockets.
# The -L/usr/ucblib -lucb gives location for the Berkeley library needed for
# the bcopy, bzero, and bcmp. The -R/usr/ucblib tells where to load
# the runtime library.
 
UNAME=$(shell uname)
LIBS= -lnsl
ifeq ($(UNAME),SunOS)
LIBS+= -lsocket
endif
 
all: udp_send udp_recv
 
udp_send: udp_send.c networks.h
        $(CC) $(CFLAGS) -o udp_send udp_send.c $(LIBS)
 
udp_recv: udp_recv.c networks.h
        $(CC) $(CFLAGS) -o udp_recv udp_recv.c $(LIBS)
 
clean:
        rm udp_recv udp_send


Again, assumes GNU make. You could also make two targets, one for Linux and one for Sun and then do a "make linux" or "make sun". The above way should be automatic though.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA


Return to Fedora/Red Hat

Who is online

Users browsing this forum: No registered users and 1 guest

cron