compiling using libnids - gcc

I have been trying to install libnids (Ubuntu LTS and Mac OS X) all the day and now I know how to compile programs with it.
I write this here because there is not much documentation but there are samples in the libnids downloaded folder with a makefile. The important things of this makefile are these:
CC = gcc
PCAPLIB = -lpcap
LNETLIB = -lnet
LIBS = -L../src -lnids $(PCAPLIB) $(LNETLIB) -lgthread-2.0 -lnsl
example:
$(CC) example.c -o example $(LIBS)
And if you're compiling it in Mac OS X ignore this: -lgthread-2.0 -lnsl -L../src
But I don't know if something stop to works because of these omitted things.

I found it. The best way is to compile the library and then use local reference to the ".a" file.
Makefile example
CC = gcc -g -Wall
GLIB = `pkg-config --cflags --libs glib-2.0`
PCAPLIB = -lpcap
LNETLIB = -lnet
LIBS_SRC = libnids-1.24/src/libnids.a
LIBS = $(PCAPLIB) $(LNETLIB) -lgthread-2.0
program: program.c
$(CC) -c $(CFLAGS) program.c -o program.o $(GLIB) $(LIBS)
$(CC) program.o -o program $(LIBS_SRC) $(GLIB) $(LIBS)
But if you want to compile the library and install it into your system you got to use -lnids instead of the above way. But the first solution always works.

I got errors when compiling samples under libnids:
gcc -o overflows overflows.o -L../src -lnids -lpcap -lnet -lgthread-2.0 -lnsl ../src/libnids.a
/usr/bin/ld: ../src/libnids.a(libnids.o): undefined reference to symbol 'g_async_queue_pop'
/lib/x86_64-linux-gnu/libglib-2.0.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: * [overflows] Error 1
You recommendation
GLIB = `pkg-config --cflags --libs glib-2.0`
solves my problem.
PS: my system Ubuntu 13.10. 64bit.

Related

How do I link a archive file with C code?

I'm trying to link a static library archive file (libx/libx.a) with a C code. The library requires 2 flags (-lx -lpthread). After linking the static library my ultimate goal is to create a shared library. I have the following Make file,
rsa-engine: rsa/rsa.c rsa/bignum.c rsa/aes.c rsa/x509parse.c rsa/pem.c
gcc -fPIC -o rsa/rsa.o -c rsa/rsa.c
gcc -fPIC -o rsa/bignum.o -c rsa/bignum.c
gcc -fPIC -o rsa/aes.o -c rsa/aes.c
gcc -fPIC -o rsa/x509parse.o -c rsa/x509parse.c
gcc -fPIC -o rsa/pem.o -c rsa/pem.c
gcc -fPIC rsa-engine.c libx/libx.a -L.libx/ -lx -lpthread -o rsa-engine.o
gcc -shared -o librsa_engine.so -lcrypto rsa-engine.o rsa/rsa.o rsa/bignum.o rsa/aes.o rsa/x509parse.o rsa/pem.o
clean:
rm -f *.o rsa/*.o *.so rsa-engine
After using the make command it produces the following output,
/usr/bin/ld: cannot find -lx
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'rsa-engine' failed
make: *** [rsa-engine] Error 1
I found similar questions here. But that did not help. Seems like I can't make the link work. Any help with what I'm doing wrong?
I would like to achieve the same result generated by the following command,
CC := gcc
CFLAGS := -Wall -g -MD -O2 -I ../
LDFLAGS := -lx -lpthread
tests_files := hello
all: $(tests_files)
hello: hello.o ../libx/libx.a
$(CC) $(CFLAGS) -static $(<) -L../libx/ $(LDFLAGS) -o $(#)
It seems that your libx.a is located in libx, yet -L references .libx directory. Anyway, since you reference libx/libx.a directly, you may skip both -L.libx/ -lx and it should link just fine.

Static library gcc - library not found

I want to create static library and something goes wrong. I have makefile:
static: main.c tree.c
gcc -c -Wall tree.c -o tree.o
ar crs libtree.a tree.o
gcc -Wall -static main.c -L. -ltree -o main
./main
When I write "make static", it shows me:
gcc -c -Wall tree.c -o tree.o
ar crs libtree.a tree.o
gcc -Wall -static main.c -L. -ltree -o main
ld: library not found for -lcrt0.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [static] Error 1
It created files: tree.o and libtree.a. I don't know why it doesn't want to find a library. Do you know how to solve it?
Most probably, your system is not set up for static linking. Most newer Linux distributions aren't as static linking is highly discouraged.
Look for a package named glibc-static or similar and install.
In case your system is not Linux (could be MacOS X as well, you didn't state that) - You're doomed. Static linking is not supported on that platform at all.

makefile linking error -lSDL (linux)

I recently tried to make a makefile and it works fine on my laptop. I was making a game in c++ and shared my code on github. One of my friends cloned
the repo and informed me that he ran into some errors. Coming to errors I must inform you that my project uses QuickCG which is based on SDL, so naturally one needs to have SDL installed. My friend did not have that initially but later downloaded it using sudo apt-get install libsdl1.2-dev. After downloading, the 'header include' error was removed but still there is linking error I suppose.
Here is my makefile
CC = g++
CFLAGS = -c -O3
CLINK_FLAGS = -lSDL
all : game
game : main.o quickcg.o
$(CC) main.o quickcg.o -o game $(CLINK_FLAGS)
main.o : main.cpp
$(CC) $(CFLAGS) main.cpp
quickcg.o : quickcg.cpp
$(CC) $(CFLAGS) quickcg.cpp
clean :
rm *.o game
My Game folder contains only 2 cpp files which are main.cpp and quickcg.cpp. It also contains 1 header file quickcg.h
Now to come to the exact error statement after executing make
g++ -c -O3 main.cpp
g++ -c -O3 quickcg.cpp
g++ main.o quickcg.o -o game -lSDL
/usr/bin/ld: cannot find -lSDL
collect2: error: ld returned 1 exit status
make: *** [game] Error 1
Please note that this worked completely fine on my laptop.
Also please note that the following command worked like a charm even on his laptop
g++ *.cpp -lSDL -O3
So clearly there seems to be a fault with makefile.
Forgive me if I have done anything stupid because I am new to makefiles but I am not able to find any errors here. Thanks in advance
You may try
CLINK_FLAGS = `sdl-config --libs`
if you don't know the path for -L.

Using X11 in OSX 10.8.3 : "ld: library not found for -lX11"

I have the following makefile:
PROG = draw
CFLAGS = -w -s -O2 -ansi -DSHM
XLIBS = -lX11 -lXext -lXmu -lXext -lXmu -lXt -lXi -lSM -lICE
LIBS = -framework OpenGL
INCLS = -I/usr/X11R/include -I/share/mesa/include
LIBDIR = -L/usr/X11/lib -L/usr/X11R6/lib
#source codes
SRCS = draw_main.cpp $(PROG).cpp
#substitute .cpp by .o to obtain object filenames
OBJS = $(SRCS:.cpp=.o)
#$< evaluates to the target's dependencies,
#$# evaluates to the target
$(PROG): $(OBJS)
g++ -o $# $(OBJS) $(LIBDIR) $(LIBS) $(XLIBS)
$(OBJS):
g++ -c $*.cpp $(INCLS)
clean:
rm $(OBJS)
Every time I try to run it, I get the following error:
ld: library not found for -lX11
collect2: ld returned 1 exit status
Can someone please help me find out where the library for X11 is supposed to be in OSX 10.8.3? To me, this looks correct.
Thanks in advance.
Have you installed XQuartz, as suggested here: http://support.apple.com/kb/HT5293 ?
By default there is no X11 on Mountain Lion.

compile with gsl with an existing makefile

i have a makefile for some code library i'm using and now i've added to that code some code that uses gsl. i'm not so sure how and what to add to the makefile (which i wat to keep since it's invoking boost as well) that would invoke gsl.
This is my makefile:
CXX = g++
ARCH = -mtune=generic
# ARCH = -march=core2
# ARCH = -march=native
COFLAGS = $(ARCH) -O3 -pipe
CXXFLAGS = -Wall $(COFLAGS)
PROGRAMS = getData analyzeData
BOOSTFLAGS = -I .
OPENMP = -fopenmp -DSUPPORT_OPENMP
all: $(PROGRAMS)
getData: getData.cpp common.o parse.o common.h
$(CXX) $(CXXFLAGS) getData.cpp common.o parse.o -o getData
analyzeData: analyzeData.cpp common.o parse.o parameters.o
$(CXX) $(CXXFLAGS) $(BOOSTFLAGS) $(OPENMP) estimateCrossReplicatesExpression.cpp common.o parse.o parameters.o -o analyzeData
parameters.o: parameters.cpp parameters.h
parse.o: parse.cpp parse.h
common.o: common.cpp common.h
clean:
rm *.o $(PROGRAMS)
In case GSL is installed on the default path (/usr/local/include/gsl) on your system, the compilation command for a source file "example.c" would be
gcc -Wall -I/usr/local/include -c example.c
The library is installed as a single file, libgsl.a. A shared version of the library libgsl.so is also installed on systems that support shared libraries. The default location of these files is /usr/local/lib. If this directory is not on the standard search path of your linker you will also need to provide its location as a command line flag.
To link against the library you need to specify both the main library and a supporting cblas library, which provides standard basic linear algebra subroutines. A suitable cblas implementation is provided in the library libgslcblas.a if your system does not provide one. The following example shows how to link an application with the library,
$ gcc -L/usr/local/lib example.o -lgsl -lgslcblas -lm
The option -lm links with the system math library. On some systems it is not needed.
Thus, you need to specify the gsl specific flags in your compile command. Update the Makefile accordingly.

Resources