I'm trying to read in data from pcap files using pcap_open_offline(). I've used #include <pcap/pcap.h> and compiled with no errors after some debugging. Now I've come across another problem I can't seem to figure out. I wrote the following function:
void openPcap(char* filename){
printf("Opening file %s\n", filename);
pcap_t *pcap;
const unsigned char *packet;
char errbuf[PCAP_ERRBUF_SIZE];
struct pcap_pkthdr header;
pcap = pcap_open_offline(filename, errbuf);
if (pcap == NULL){
fprintf(stderr, "%s Malformed packet records in file %s",ER,filename);
}
}
And my pcap_open_offline call gives me about 100 of these errors when I try to compile:
pcap-linux.c:(.text+0xcd4): undefined reference to 'nl_handle_alloc'
pcap-linux.c:(.text+0xce8): undefined reference to 'genl_connect'
pcap-linux.c:(.text+0xcf6): undefined reference to 'genl_ctrl_alloc_cache'
pcap-linux.c:(.text+0xd0e): undefined reference to 'genl_ctrl_search_by_name'
pcap-linux.c:(.text+0xd64): undefined reference to 'nl_handle_destroy'
pcap-linux.c:(.text+0xdd7): undefined reference to 'nl_cache_free'
This is what my makefile looks like:
# -------------------------------
C=/afs/nd.edu/user14/csesoft/new/bin/gcc
CFLAGS=-Wall -std=c11 -I/afs/nd.edu/coursesp.18/cse/cse30341.01/support/gcc-libpcap/include -D_BSD_SOURCE
LD=/afs/nd.edu/user14/csesoft/new/bin/g++
#LD=g++
LDFLAGS=-lpthread
# # ----------------------------
LDFLAGS += -L/afs/nd.edu/coursesp.18/cse/cse30341.01/support/gcc-libpcap/lib -lpcap # Add your own flags here, or leave blank
threadedRE: threadedRE.o
$(LD) $^ $(LDFLAGS) -o $#
threadedRE.o: threadedRE.c
$(C) $(CFLAGS) -c $<
# C compiler
%.o: %.c
$(C) $(CFLAGS) -c $<
.PHONY: clean
clean:
rm -f threadedRE *.o
And my headers are:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <sys/stat.h>
#include <pcap/pcap.h>
Any suggestions would be greatly appreciated.
It isn't a compiling error, but a linking one. At the end of build process you should see something like "ld exited with error".
pcap_open_offline() seems to use nl_handle_alloc() and other functions, but linker can't find object files containing their implementation. Pointing linker to proper library which contains required object files by adding -lnl to LDFLAGS should do the trick.
Related
My environment is Lua-5.4.2 Luasocket-3.0-rc1.
When I run lua script directly, it work success.
When i run it through c language, it tell me error.
Error Msg is :
PANIC: unprotected error in call to Lua API (error running script: error loading module 'socket.core' from file '/usr/local/lib/lua/5.4/socket/core.so': undefined symbol: lua_gettop)
Aborted(core dumped)
Does anyone know why?
lua script code is:(test.lua)
#!/usr/local/bin/lua
local socket = require("socket")
print(socket._VERSION)
c code is:(main.c)
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int main(void)
{
lua_State *L;
L = luaL_newstate();
luaopen_base(L);
luaL_openlibs(L);
printf("lua enter\n");
if (luaL_dofile(L, "test.lua"))
{
luaL_error(L, "error running script: %s", lua_tostring(L, -1));
}
printf("lua exit\n");
while(1) pause();
lua_close(L);
return 0;
}
tl;dr: Pass -Wl,-E to GCC.
I was able to reproduce your problem with this Dockerfile:
FROM gcc:11.1.0
RUN wget https://www.lua.org/ftp/lua-5.4.2.tar.gz \
&& git clone https://github.com/diegonehab/luasocket.git
RUN tar zxf lua-5.4.2.tar.gz \
&& cd lua-5.4.2 \
&& make linux \
&& make install \
&& cd ../luasocket \
&& git checkout 5b18e475f38fcf28429b1cc4b17baee3b9793a62 \
&& make linux LUAV=5.4 \
&& make install LUAV=5.4
COPY test.lua main.c ./
When I run lua test.lua in the resulting Docker container, it works fine. When I run gcc -o test main.c /usr/local/lib/liblua.a -ldl -lm -Wl,-rpath='/usr/local/lib/lua/5.4/socket' && ./test, I get the same panic that you get.
The reason that the standalone Lua binary works and yours doesn't is that the former is linked with -E:
MYLDFLAGS= $(LOCAL) -Wl,-E
ld's documentation for -E says this about it:
If you use dlopen to load a dynamic object which needs to refer back to the symbols defined by the program, rather than some other dynamic object, then you will probably need to use this option when linking the program itself.
Lua uses dlopen to load C modules that you require, and said modules need to call Lua functions, which are linked into your binary, so it makes sense that you need this option. And indeed, when I add -Wl,-E to the GCC command line, then it works fine for me:
root#077bbb831441:/# gcc -o test main.c /usr/local/lib/liblua.a -ldl -lm -Wl,-rpath='/usr/local/lib/lua/5.4/socket' -Wl,-E && ./test
lua enter
LuaSocket 3.0-rc1
lua exit
I am trying to compile the files after generating from these steps:-
(1) wsdl2h -o calc.h http://www.genivia.com/calc.wsdl
(2) soapcpp2 -j -CL calc.h
(3) Creating a main.cpp with the following code:-
#include "calc.nsmap" // XML namespace mapping table (only needed once at the global level)
#include "soapcalcProxy.h" // the proxy class, also #includes "soapH.h" and "soapStub.h"
int main()
{
calcProxy calc;
double sum;
if (calc.add(1.23, 4.56, sum) == SOAP_OK)
std::cout << "Sum = " << sum << std::endl;
else
calc.soap_stream_fault(std::cerr);
calc.destroy(); // same as: soap_destroy(calc.soap); soap_end(calc.soap);
}
After it I compile issuing the command:-
g++ -o calcclient main.cpp soapcalcProxy.cpp soapC.cpp -lgsoap++
I get the following errors:-
/tmp/ccA5Ergj.o: In function `soap_ignore_element(soap*)':
soapC.cpp:(.text+0x112d): undefined reference to `soap_ignore'
/tmp/ccA5Ergj.o: In function `soap_putelement':
soapC.cpp:(.text+0x149b): undefined reference to `soap_element_empty'
collect2: error: ld returned 1 exit status
Please help in compiling.
I solved this. I was thinking the lib is in /usr/lib but it was in /usr/local/lib. I included -L/usr/local/lib while compiling, and it worked.
I am trying to learn glib and gObject for a networking project.
Here is the command used to compile (after using pkg-config to get output):
gcc socket1.c -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lgobject-2.0 -lglib-2.0
socket1.c, after stripping, to identify the problem is:
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <gio/gio.h>
static GSocket *mySocket;
int main(int argc, char **argv) {
GError *err1 = NULL;
mySocket = g_socket_new ( G_SOCKET_FAMILY_IPV4,
G_SOCKET_TYPE_STREAM,
G_SOCKET_PROTOCOL_TCP,
&err1);
}
The error is:
/tmp/ccKIEXOi.o: In function `main':
socket1.c:(.text+0x3d): undefined reference to `g_socket_new'
collect2: error: ld returned 1 exit status
I have tried to use readelf -Ws and nm to see if I could see g_new_socket in any of the .so files linked. I did not see any. Is there a separate library I need to link to? Where/which is it?
You need to link to libgio-2.0.so, which is the third part of GLib (GLib, GObject, GIO). You can get its compiler and linker flags from pkg-config using pkg-config --cflags --libs gio-2.0.
You can tell GSocket is in GIO by looking at the documentation — it’s in the GIO manual: https://developer.gnome.org/gio/stable/GSocket.html.
I am trying to use OPENCV 3.1 from inside of the caffe . This is my test code
#include <caffe/caffe.hpp>
#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#endif // USE_OPENCV
#include<iostream>
#ifdef USE_OPENCV
int main(){
std::cout<<"USE_OPENCV value is 1\n";
return 0;
}
#else
int main(){
std::cout<<"USE_OPENCV value is 0\n";
return 0;
}
#endif
I am compiling it using following command
g++ -I path_to_caffe/distribute/include/ test3.cpp -std=c++0x -lboost_system
It compiles with giving the following output while executing :
USE_OPENCV value is 0
Before compiling the caffe i make this changes in Makefile.config file:
USE_OPENCV := 1
. . .
OPENCV_VERSION := 3
. . .
USE_PKG_CONFIG := 1
While compiling caffe, I firstly cleand it and then compiled it using following commands:
make clean
make all -j $(($(nproc) + 1))
make test
make runtest
make pycaffe
make distribute
It compiles without giving error , but while testing my test file it does not gives the output that i expect.
All glories goes to Shai. The solution is to add -DUSE_OPENCV flag .
g++ -I path_to_caffe/distribute/include/ test3.cpp -std=c++0x -lboost_system -DUSE_OPENCV
I have a problem with the linking simpliest test program in QTCreator:
CODE:
#include <QtCore/QCoreApplication>
#include <cv.h>
#include <highgui.h>
#include <cxcore.hpp>
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat M(7,7,CV_32FC2,Scalar(1,3));
return 0;
}
.pro file:
QT -= gui
TARGET = testopencv
CONFIG += console
CONFIG -= app_bundle
INCLUDEPATH += C:/OpenCV2_1/include/opencv
TEMPLATE = app
LIBS += C:/OpenCV2_1/lib/cxcore210d.lib \
C:/OpenCV2_1/lib/cv210d.lib \
C:/OpenCV2_1/lib/highgui210d.lib\
C:/OpenCV2_1/lib/cvaux210d.lib
SOURCES += main.cpp
I've tried to use -L and -l like LIBS += -LC:/OpenCV2_1/lib -lcxcored
And in .pro file:
QMAKE_LIBDIR += C:/OpenCV2_1/lib/Debug
LIBS += -lcxcore210d \
-lcv210d \
-lhighgui210d
The errors are like:
debug/main.o:C:\griskin\test\app\testopencv/../../../../OpenCV2_1/include/opencv/cxcore.hpp:97: undefined reference to cv::format(char const*, ...)'
Could anyone help me? Thanks!
In Visual Studio it works but I need it works in QTCreator..
Qt uses the MinGW compiler and linker. It will happily link OpenCV .libs when you use the C interface. However due to ABI issues, it will not link C++ modules.
You will have to either restrict yourself to the C interfaces only (i.e. no cv::Mat), or get OpenCV to compile with MinGw (or compile Qt in/with VS). Also, see this thread.
This is what my .pro file looks like
INCLUDEPATH += C:\\opencv\\release\\install\\include\
LIBS += -LC:\\opencv\\release\\install\\bin \
-lopencv_core240 \
-lopencv_highgui240 \
-lopencv_imgproc240 \
-lopencv_features2d240 \
-lopencv_calib3d240 \
and replace
#include <cv.h>
with
#include <opencv/cv.h>
Above .pro file works perfect for me. I have used mingw to compile OpenCV and using mingw compiler tool chain in Qt.
It seems that QtCreator cannot locate lib files.
Try specifying the include file and libs as follows.
INCLUDEPATH += C:/OpenCV2_1/build/include/
LIBS += C:/OpenCV2_1/build/gpu/x86/lib/cxcore210d.lib
you should refer to build folder if you're using pre-build version of opencv.
(I recommend pre-build version if you're not that familiar with opencv)