I've searched about this a lot. And tried a lot. Just don't know where I go wrong.
Here is my code, it's very simple:
#include <glib.h>
int main()
{
int *ip=g_new(int,1);
*ip=42;
return *ip;
}
First I try apt-get libglib2.0-dev in my Ubuntu and Mint, when it's done, compile with:
gcc `pkg-config --cflags --libs glib-2.0` -o main main.c
/tmp/ccYFljQD.o: In function `main':
main.c:(.text+0x13): undefined reference to `g_malloc_n'
collect2: error: ld returned 1 exit status
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 1
And the output of pkg-config:
$ pkg-config --cflags --libs glib-2.0
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0
So it's not working, then I try to compile from source and install one by my own.
I've apt-get libffi-dev, autogen and configure, make, make install, that's all okay.
$ pkg-config --cflags --libs glib-2.0
-I/home/donpope/software/include/glib-2.0 -I/home/donpope/software/lib/glib-2.0/include -L/home/donpope/software/lib -lglib-2.0
Yet compile with the same error:
gcc `pkg-config --cflags --libs glib-2.0` -o main main.c
/tmp/cctR3iEq.o: In function `main':
main.c:(.text+0x13): undefined reference to `g_malloc_n'
collect2: error: ld returned 1 exit status
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 1
So I need some help here. Thank you!
Update:
Later I try this in a RedHat with older gcc. And it's just okay.
I have this Makefile in my glib sandbox:
PKGS=glib-2.0
CFLAGS+=$(shell pkg-config --cflags $(PKGS))
LDFLAGS+=$(shell pkg-config --libs $(PKGS))
%: %.c
$(CC) $(CFLAGS) $< -o $# $(LIBS) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) $< -c -o $#
%: %.o
$(CC) $< -o $# $(LIBS) $(LDFLAGS)
Starting from this and tweaking, you should be able to figure out the exact commandline and compile your program.
Also, you should make sure that your PKG_CONFIG_PATH is properly updated to contain the path where you installed glib (typically in PREFIX/lib/pkgconfig).
Ubuntu 18.04 here. I had to place all my glib flags at the end of the compile statement, even after the source .c file. So this compiles fine:
gcc -Wall -o bfs bfs.c `pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0`
but this does not :
gcc -Wall -o bfs `pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0` bfs.c
/tmp/ccvWsLk3.o: In function `newGraph':
bfs.c:(.text+0x58): undefined reference to `g_hash_table_new'
collect2: error: ld returned 1 exit status
Which drove me nuts for a while as I was taught that it's always good style to place the source file at the end of the compile statement. :p
Related
I got an error while doing Makefile in jetson nano
This is an error message.
chmod +x *.sh
g++ -std=c++11 -std=c++11 -Iinclude/ -I3rdparty/stb/include -DOPENCV_DNN `pkg-config --cflags libconfig++` `pkg-config --cflags libcurl` `pkg-config --cflags sqlite3` `pkg-config --cflags libxml-2.0` `pkg-config --cflags libhttpserver` `pkg-config --cflags live555` `pkg-config --cflags libswscale` `pkg-config --cflags x264` -DOPENCV `pkg-config --cflags opencv4 2> /dev/null || pkg-config --cflags opencv` -DGPU -I/usr/local/cuda/include/ -DCUDNN -DCUDNN_HALF -Wall -Wfatal-errors -Wno-unused-result -Wno-unknown-pragmas -fPIC -Ofast -DOPENCV_DNN -DRTSP -DOPENCV -DGPU -DCUDNN -I/usr/local/cudnn/include -DCUDNN_HALF -fPIC obj/image_opencv.o obj/http_stream.o obj/gemm.o obj/utils.o obj/dark_cuda.o obj/convolutional_layer.o obj/list.o obj/image.o obj/activations.o obj/im2col.o obj/col2im.o obj/blas.o obj/crop_layer.o obj/dropout_layer.o obj/maxpool_layer.o obj/softmax_layer.o obj/data.o obj/matrix.o obj/network.o obj/connected_layer.o obj/cost_layer.o obj/parser.o obj/option_list.o obj/darknet.o obj/detection_layer.o obj/captcha.o obj/route_layer.o obj/writing.o obj/box.o obj/nightmare.o obj/normalization_layer.o obj/avgpool_layer.o obj/coco.o obj/dice.o obj/yolo.o obj/detector.o obj/layer.o obj/compare.o obj/classifier.o obj/local_layer.o obj/swag.o obj/shortcut_layer.o obj/activation_layer.o obj/rnn_layer.o obj/gru_layer.o obj/rnn.o obj/rnn_vid.o obj/crnn_layer.o obj/demo.o obj/tag.o obj/cifar.o obj/go.o obj/batchnorm_layer.o obj/art.o obj/region_layer.o obj/reorg_layer.o obj/reorg_old_layer.o obj/super.o obj/voxel.o obj/tree.o obj/yolo_layer.o obj/gaussian_yolo_layer.o obj/upsample_layer.o obj/lstm_layer.o obj/conv_lstm_layer.o obj/scale_channels_layer.o obj/sam_layer.o obj/convolutional_kernels.o obj/activation_kernels.o obj/im2col_kernels.o obj/col2im_kernels.o obj/blas_kernels.o obj/crop_layer_kernels.o obj/dropout_layer_kernels.o obj/maxpool_layer_kernels.o obj/network_kernels.o obj/avgpool_layer_kernels.o obj/pw_encrypt.o -o darknet -lm -pthread `pkg-config --libs libconfig++` `pkg-config --libs libcurl` `pkg-config --libs sqlite3` `pkg-config --libs libxml-2.0` `pkg-config --libs libhttpserver` `pkg-config --libs live555` `pkg-config --libs libswscale` `pkg-config --libs x264` `pkg-config --libs opencv4 2> /dev/null || pkg-config --libs opencv` -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand -L/usr/local/cudnn/lib64 -lcudnn -lstdc++ -lcrypt
Package sqlite3 was not found in the pkg-config search path.
Perhaps you should add the directory containing `sqlite3.pc'
to the PKG_CONFIG_PATH environment variable
No package 'sqlite3' found
Package sqlite3 was not found in the pkg-config search path.
Perhaps you should add the directory containing `sqlite3.pc'
to the PKG_CONFIG_PATH environment variable
No package 'sqlite3' found
obj/parser.o: In function `parse_implicit':
parser.c:(.text+0x4b54): undefined reference to `make_implicit_layer'
obj/parser.o: In function `save_implicit_weights':
parser.c:(.text+0x85d4): undefined reference to `pull_implicit_layer'
obj/parser.o: In function `load_implicit_weights':
parser.c:(.text+0x9b84): undefined reference to `push_implicit_layer'
collect2: error: ld returned 1 exit status
Makefile.nano:249: recipe for target 'darknet' failed
make: *** [darknet] Error 1
I set it up as follows and compiled it, but an error occurred. Do you know what the problem is?
GPU=1
CUDNN=1
CUDNN_HALF=1
OPENCV=1
AVX=0
OPENMP=0
LIBSO=1
OPENCV_DNN=1
TENSORRT=0
RTSP=1
1. Copy the cudNN files that are part of jetpack to the correct location for darknet to use /usr/local/cuda/
# add the cudnn header file to the include folder
sudo cp /usr/include/cudnn.h /usr/local/cuda/include
# copy the cudnn shared object library files to the cuda library folder
sudo cp -P /usr/lib/aarch64-linux-gnu/libcudnn* /usr/local/cuda/lib64
# change the ownership and read writes for the jetson nano user
sudo chmod a+r /usr/local/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
2. Compile darknet using build_darknet_nano.sh
Turns out that src/representation_layer.c wasn't getting compiled and that's why the weird error was turning up saying that it can't find the functions. I solved this by including the c file in the build commands.
I have created this build_darknet_nano.sh script which you can run to compile darknet on jetson nano:
https://gist.github.com/Raunak-Singh-Inventor/4f1a20808bf2579ec938f8e050bf6514
To run this:
save this code in a file called build_darknet_nano.sh
chmod u+x build_darknet_nano.sh
./build_darknet_nano.sh
And there you go. That should succesfully compile darknet on the Jetson Nano. Try running ./darknet to see if everything works.
i want to compile opencv4.0 with Makefile but undefined reference error occur.
i have used to opencv in Windows and code is just simple code that only show image for test in ubuntu18.10.
but it work if i typing line below on shell.
g++ -o simple main.cpp $(pkg-config opencv4 --libs --cflags)
my Makefile is below
CC = g++
CFLAGS = -W -Wall
SRCS = main.cpp
TARGET = simple
OPENCV = $(pkg-config opencv4 --libs --cflags)
LIBS = $(OPENCV)
$(TARGET):$(SRCS)
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LIBS)
clean:
rm -f $(OBJECTS) $(TARGET) core
and my opencv4.pc is below.
# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.0.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_b
Libs.private: -ldl -lm -lpthread -lrt -L/usr/lib/x86_64-linux-gnu -lGL -lGLU
Cflags: -I${includedir}
and error is below.
g++ -W -Wall -o simple main.cpp
/usr/bin/ld: /tmp/cciHsvhP.o: in function `main':
main.cpp:(.text+0x70): undefined reference to `cv::imread(cv::String const&, int)'
/usr/bin/ld: main.cpp:(.text+0xc4): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
....
collect2: error: ld returned 1 exit status
make: *** [Makefile:11: simple] Error 1
I assume you're using GNU Make, since you are working on Ubuntu Linux.
I also assume what you have posted as:
$(TARGET):$(SRCS)
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LIBS)
is mis-formatted in the posting and that your Makefile really contains:
$(TARGET):$(SRCS)
(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LIBS)
with the recipe command tab-indented as it must be.
In your Makefile you believe that:
OPENCV = $(pkg-config opencv4 --libs --cflags)
is a shell-expansion of the command pkg-config opencv4 --libs --cflags that
assigns the output of the command to the Make variable OPENCV. It is not.
It is simply a Make expansion of the string pkg-config opencv4 --libs --cflags,
just as in the next line:
LIBS = $(OPENCV)
$(OPENCV) is the Make expansion of the Make variable OPENCV and not the shell
expansion of a shell command OPENCV.
The string pkg-config opencv4 --libs --cflags is not a Make variable that has a value (obviously).
Neither can it be an invocation of a GNU Make-function
$(pkg-config ...), as there is no such GNU Make function.
So $(pkg-config opencv4 --libs --cflags) expands to nothing. Hence:
LIBS = $(OPENCV)
makes $(LIBS) expand to nothing, and:
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LIBS)
expands to the same as:
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS)
which is why the opencv libraries are missing from your linkage and the linkage fails.
To assign the output of a shell command to a Make variable, using the $(shell ...) function:
OPENCV := $(shell pkg-config opencv4 --libs --cflags)
Then $(OPENCV) and $(LIBS) will acquire the correct value.
BTW...
Note that your clean recipe attempts to delete a file, core, that is never created by your Makefile.
And..
Be aware that the recipe:
$(TARGET):$(SRCS)
(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LIBS)
represents in general the most inefficient possible way of automating a
program's build process with Make, because it will recompile all of the N
source files in $(SRCS) whenever you change even 1 of them. In your particular
case as posted, it doesn't matter because N = 1. But when N = 1 there is no
need for Make. In anticipation of writing more professional projects where N is large, you
might like to work through Chapter 2 An Introduction to Makefiles,
at least, in the GNU Make manual.
This part of your makefile is likely broken:
$(TARGET):$(SRCS)
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LIBS)
It looks like a leading tab character is missing on the second line.
It is not used, so make uses the default rule for building executables from .cpp files, which does not use the LIBS variable (only CFLAGS).
I am trying to compile a short test program that requires Qt4, but have trouble properly linking to the Qt4 library. I have installed Qt4 via
sudo apt-get install qt4-dev-tools
The program code looks like this
#include <QtCore>
#include <iostream>
int main() {
std::cout << "Qt version: " << qVersion() << std::endl;
}
The shared library libQtCore.so is at /usr/lib/x86_64-linux-gnu but trying to compile the following way
g++ -L/usr/lib/x86_64-linux-gnu -Wall -o test.exe test.cpp -lQtCore
returns an error message that there is no file or directory called QtCore.
I have also tried to directly use the QtCore source code, but have received the following error message:
/tmp/ccljEHOY.o: In function main':
test.cpp:(.text+0xa): undefined reference toqVersion()'
collect2: error: ld returned 1 exit status
What am I doing wrong here?
Thanks
Ips
The compiler was not able to find QtCore file because you need to add path with Qt to the list of directories to be searched for header files.
You can use the pkg-config to get proper flags:
$:pkg-config QtCore --cflags
-DQT_SHARED -I/usr/include/qt4 -I/usr/include/qt4/QtCore
For linking:
$:pkg-config QtCore --libs
-lQtCore
You can use pkg-config when calling the compiler:
g++ test.cpp `pkg-config QtCore --cflags --libs`
or
g++ `pkg-config QtCore --cflags` test.cpp `pkg-config QtCore --libs`
Note that the following way won't work:
g++ `pkg-config QtCore --cflags --libs` test.cpp
I have written an OpenCV program which contains both main.cpp and pedestrian.cpp. I wanted to compile them, so I did the following:
g++ -c -Wall `pkg-config --cflags opencv` main.cpp -o main.o
g++ -c -Wall `pkg-config --cflags opencv` pedestrian.cpp -o pedestrian.o
g++ `pkg-config --libs opencv` pedestrian.o main.o -o detect
After the third statement, every OpenCV command I use is considered an undefined reference. I have no idea why this is happening since other single object programs work just fine with pkg-config. I checked to see if it was a namespace problem and it wasn't either. Any help would be greatly appreciated.
Try changing the order int the last line to:
g++ -o detect pedestrian.o main.o `pkg-config --libs opencv`
This should work. Order matters when linking the .o files. When the linker finds and unknown OpenCV symbol in any of the .o, it looks for its definition in the following linked elements (i.e. in the elements to the right).
I want to use OpenCV as a static library. After compiling the library as a static library now i'm testing with some examples to see all the dependencies.
The dependencies added to compile are -staticpkg-config --libs opencv-lpthread -ljpeg -ltiff -lz -ljasper -lpng12
But obtain the following Error message:
g++ -c -g -Wall -O2 `pkg-config --cflags opencv` formas.cpp
g++ -g -Wall -O2 `pkg-config --cflags opencv` formas.o -o paint -static `pkg-config --libs opencv` -lpthread -ljpeg -ltiff -lz -ljasper -lpng12
/usr/local/lib/libopencv_core.a(system.o): In function `cv::tempfile(char const*)':
system.cpp:(.text._ZN2cv8tempfileEPKc+0x3a): warning: the use of `tmpnam' is dangerous, better use `mkstemp'
/usr/local/lib/libopencv_core.a(system.o): In function `cv::getTickCount()':
system.cpp:(.text._ZN2cv12getTickCountEv+0x2a): undefined reference to `clock_gettime'
/usr/local/lib/libopencv_highgui.a(grfmt_png.o): In function `cv::PngDecoder::readHeader()':
grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0xde): undefined reference to `png_set_longjmp_fn'
/usr/local/lib/libopencv_highgui.a(grfmt_png.o): In function `cv::PngEncoder::write(cv::Mat const&, std::vector<int, std::allocator<int> > const&)':
grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x134): undefined reference to `png_set_longjmp_fn'
/usr/local/lib/libopencv_highgui.a(grfmt_png.o): In function `cv::PngDecoder::readData(cv::Mat&)':
grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x141): undefined reference to `png_set_longjmp_fn'
collect2: ld returned 1 exit status
make: *** [all] Error 1
I made something wrong or there an error in OpenCV library.
OpenCV version: 2.3.1 (latest version)
formas --> example which create an image an draw some figures and save into a file. So simple.
Thanks in advance.
It seems you have compiled OpenCV with libpng 1.4 or newer but trying to link with libpng 1.2
What cmake command have you used to configure OpenCV?
Update:
You need to add -lrt to the list of libraries you link for the clock_gettime function.