I tried to build this, but always got link-time error.
#include <libavutil/log.h>
int main(int argc, char *argv[])
{
::av_log_set_flags(AV_LOG_SKIP_REPEATED);
return 0;
}
My distro is Debian GNU/Linux 8 (jessie). The FFmpeg was built by myself, and the configure command was...
$ ./configure --prefix=/usr/local --disable-static --enable-shared \
> --extra-ldflags='-Wl,-rpath=/usr/local/lib'
The link-error is as follows.
$ g++ foo.cpp -D__STDC_CONSTANT_MACROS -Wall \
> -Wl,-rpath=/usr/local/lib \
> $(pkg-config --cflags --libs libavutil)
/tmp/ccKzgEFb.o: In function `main':
foo.cpp:(.text+0x17): undefined reference to `av_log_set_flags(int)'
collect2: error: ld returned 1 exit status
where the output of pkg-config is...
$ pkg-config --cflags --libs libavutil
-I/usr/local/include -L/usr/local/lib -lavutil
The objdump shows that the shared object libavutil.so does have av_log_set_flogs inside.
$ objdump --dynamic-syms /usr/local/lib/libavutil.so | grep 'av_log_set_flags'
000260f0 g DF .text 0000000a LIBAVUTIL_54 av_log_set_flags
Please note that the g++ command used to build the above application had a linker option -Wl,-rpath=/usr/local/lib, though it still doesn't work. Also, I've tried to monitor with inotifywait if the other version provided by the distro were called. They were not, and the one being opened during execution of g++ was /usr/local/lib/libavutil.so.
Summary:
/usr/local/lib/libavutil.so does have the symbol.
-rpath was used to force to link against the shared library.
Why link-time error? T_T
Any suggestion or information would be highly appreciated! Thanks!
REEDIT: ffplay works fine and ldd shows it use /usr/local/lib/libavutil.so. So, the libraries seems not broken, and the problem becomes how to build my own codes to use the libraries.
This had me baffled too for a while. I managed to google this: http://soledadpenades.com/2009/11/24/linking-with-ffmpegs-libav/
It turns out FFMPEG don't make their header files C++ aware.
Here is the fix:
extern "C"
{
#include <libavutil/log.h>
}
int main(int argc, char *argv[])
{
::av_log_set_flags(AV_LOG_SKIP_REPEATED);
return 0;
}
You need to wrap all ffmpeg header includes with extern "C" linkage.
Related
I've tried to use libqmi but I can't get through the linker. It keeps saying "undefined reference" on libqmi functions. Any suggestions what is needed?
Paths and libraries are available for gcc, the symbols are inside libqmi-glib, looks like everything is in place.
The code is the simplest possible, I think.
int main(int argc, char **argv)
{
GFile *qmi = g_file_new_for_path("/dev/cdc-wdm0");
printf("%li\r\n", (long int)(qmi));
g_object_unref(qmi);
return 0;
}
And the build goes like this:
gcc -I/usr/local/include/libqmi-glib/ -I/usr/include/glib-2.0/ -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/ whatever.c -L/usr/local/lib/ -L/usr/lib/x86_64-linux-gnu/ -lqmi-glib -lglib-2.0
You're missing the link to libgobject-2 and libgio-2.
Anyway, the best way to compile a program using libqmi is to use pkg-config as that knows all the cflags and ldflags you should be using; e.g. you could probably do this, assuming you installed libqmi in /usr/local:
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig gcc $(pkg-config --cflags qmi-glib) whatever.c $(pkg-config --libs qmi-glib)
I've installed the libglfw3-dev:amd64 package on Ubuntu using the standard sudo apt get etc. My following compiling line is:
g++ -o output -IL/usr/lib/x86_64-linux-gnu -lglfw driver.o
My current c++ file is:
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
if (!glfwInit())
return -1;
}
I've tried using local libraries of glfw and setting the -I and -L locations but nothing has seemed to work. I've made sure the .so and .h files are in their respective locations but I always get this error while running make:
g++ -o output -I/usr/include/GLFW -L/usr/lib/x86_64-linux-gnu -lglfw
driver.o
driver.o: In function `main':
driver.cpp:(.text+0x5): undefined reference to `glfwInit'
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'output' failed
make: *** [output] Error 1
I've tried looking at all the other SO posts and they recommend compiling with tons of extra flags, but the only thing I've been able to draw from them is that something is wrong with my library since VScode detects the .h files. How can I compile this without any errors?
Have you tried swapping the linker arguments around? That is, compile with
g++ -o output driver.o -lglfw
The linker goes through the files from left to right, and it has to know which symbols from libraries you need, before the libraries are processed.
All is perfectly explained in the manual https://www.glfw.org/docs/latest/build_guide.html#build_link_pkgconfig
The key problem is in your -I/usr/include/GLFW and #include <GLFW/glfw3.h> that gives in sum the path /usr/include/GLFW/GLFW/glfw3.h. I suppose this is a wrong path to glfw3.h. compilation was successful because of the system default include path -I/usr/include.
Do not tune compiler flags manually, let pkg-config do
it for you.
A typical compile and link command-line when using the static version of the GLFW library may look like this:
g++ -o output `pkg-config --cflags glfw3` yourprog.c `pkg-config --static --libs glfw3`
If you are using the shared version of the GLFW library, simply omit the --static flag.
g++ -o output `pkg-config --cflags glfw3` yourprog.c `pkg-config --libs glfw3`
I'm trying to make a C++ script that will run some simple Python code:
// t.cpp
#include <Python.h>
int main(int argc, char* argv[])
{
Py_Initialize();
PyRun_SimpleString("print('TEST PASSED')");
Py_Finalize();
return 0;
}
Upon running g++ t.cpp, I get the error:
t.cpp:1:20: fatal error: Python.h: No such file or directory
compilation terminated
I've found many similar questions, all specific to an IDE or other development software, or were solved by installing python3-dev. The python3-dev package is already installed, and I even tried manually including the header when attempting to compile:
g++ t.cpp -I ~/.virtualenvs/MainEnv/include/python3.5m/Python.h
g++ t.cpp -I /usr/include/python3.5m/Python.h
Neither changes anything.
How can I fix this error?
UPDATE: I found that using g++ t.cpp -I /usr/include/python3.5/ seems to include the header, but then it runs into more errors:
t.cpp:(.text+0x10): undefined reference to `Py_Initialize'
t.cpp:(.text+0x1f): undefined reference to `PyRun_SimpleStringFlags'
t.cpp:(.text+0x24): undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status
I've set up a similar example on my github
g++ t.cpp is missing a few things:
Tell g++ where the headers are for cpython (by -I/path/to/headers/)
Tell g++ to link against libpython (by -lpython3.5m)
You can also retrieve these flags with pkg-config
$ pkg-config python-3.5 --libs --cflags
-I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m -lpython3.5m
Your commandline should look something like g++ -I/usr/include/python3.5m t.cpp -lpython3.5m
#include <...> is for includes that come with the compiler.
Use #include "Python.h" for any other includes.
Run the following commands to compile your code:
mytest.cpp:
#include <Python.h>
int main(int argc, char* argv[])
{
Py_Initialize();
PyRun_SimpleString("print('TEST PASSED')");
Py_Finalize();
return 0;
}
Compile:
$ g++ mytest.cpp `pkg-config python3-embed --libs --cflags` -o mytest
$ ./mytest
I want to use the C++ API for graphicsmagick
I need to convert image data directly from OpenCV and use graphicsmagick to save the file as tiff with group 4 compression
The command line
gm convert input -type bilevel -monochrome -compress group4 output.tif
Could anyone provide some code (see the above command line) to simply convert the output from OpenCV to tiff with group 4 compression
I'm new to C++ :)
testing graphicsmagick
I'm trying to make graphicsmagick work. Found a very simple code in the docs
I can't find Magick++.h
locate /Magick++.h returns nothing
but graphicsmagick is installed
# gm -version
GraphicsMagick 1.3.20 2014-08-16 Q8 http://www.GraphicsMagick.org/
code
/*
* Compile
* g++ gm_test.cpp -o gm_test `GraphicsMagick++-config --cppflags --cxxflags --ldflags --libs`
*/
#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int argc, char **argv){
InitializeMagick(*argv);
Image image( "100x100", "white" );
image.pixelColor( 49, 49, "red" );
image.write( "red_pixel.png" );
return 0;
}
compile
# g++ gm_test.cpp -o gm_test `GraphicsMagick++-config --cppflags --cxxflags --ldflags --libs`
-bash: GraphicsMagick++-config: command not found
gm_test.cpp:6:22: fatal error: Magick++.h: No such file or directory
#include <Magick++.h>
^
compilation terminated.
Updated Answer
Try looking for a file called GraphicsMagick-config under the directory where you installed GraphicsMagick like this:
find /usr -name "GraphicsMagick-config"
When you find that, you can ask it to tell you the compiler include flags and linker flags like this:
/usr/some/path/GraphicsMagick-config --cflags --libs
Then you can compile with:
gcc $(/usr/some/path/GraphicsMagick-config --cflags --libs) somefile.c -o somefile
Original Answer
Look in the directory where you installed GraphicsMagick for a file ending in .pc, which is the pkg-config file, e.g.
find /usr/local -iname "graphic*.pc"
Then pass this file to pkg-config to get the CFLAGS and LIBS you should use for compiling. So, if your graphicsmagick.pc is in /usr/local/Cellar/graphicsmagick/1.3.23/lib/pkgconfig/GraphicsMagick.pc, use:
pkg-config --cflags --libs /usr/local/Cellar/graphicsmagick/1.3.23/lib/pkgconfig/GraphicsMagick.pc
which will give you this:
/usr/local/Cellar/graphicsmagick/1.3.23/lib/pkgconfig/GraphicsMagick.pc
-I/usr/local/Cellar/graphicsmagick/1.3.23/include/GraphicsMagick -L/usr/local/Cellar/graphicsmagick/1.3.23/lib -lGraphicsMagick
Then you would compile with:
gcc $(pkg-config --cflags --libs somefile.c -o somefile
i don't know if it's helpful, last day i have the same error :no magick++.h when i compile ImageMagick (not graphicsmagick).
so i follows the steps in a official website to reinstall ImageMagick and finally i succeed.web:
1 http://www.imagemagick.org/script/install-source.php
2 http://www.imagemagick.org/script/magick++.php
i download the latest source code(ImageMagick6.9) in centOS-6.5
and then ./configure, make, make install.
i hope it's helpful.
On Ubuntu the GraphicsMagick++-config program you are using to get compile flags is correctly part of the same package which includes Magick++.h. Trying to run it tell you where to find it:
$ g++ gm_test.cpp -o gm_test `GraphicsMagick++-config --cppflags --cxxflags --ldflags --libs`
The program 'GraphicsMagick++-config' is currently not installed. You can install it by typing:
sudo apt-get install libgraphicsmagick++1-dev
gm_test.cpp:6:22: fatal error: Magick++.h: No such file or directory
compilation terminated.
So do what it says:
$ sudo apt-get install libgraphicsmagick++1-dev
Try the compile again and you will get a different error because GraphicsMagick++-config is linking to an uninstalled and unneeded library:
$ g++ gm_test.cpp -o gm_test `GraphicsMagick++-config --cppflags --cxxflags --ldflags --libs`
/usr/bin/ld: cannot find -lwebp
collect2: error: ld returned 1 exit status
You can manually specify the libs and the compile and link works:
$ g++ gm_test.cpp -o gm_test -I/usr/include/GraphicsMagick -Wall -g -fno-strict-aliasing -O2 -pthread -lGraphicsMagick++ -lGraphicsMagick -ljbig
$ ./gm_test
Or you can install the required library:
$ sudo apt-get install libwebp-dev
I've just updated my system from ubuntu 11.04 to 11.10 and now I can't compile anymore any C program that contain references to OpenCV libraries
I've already tried to reinstall OpenCV (I use the 2.1 version) but I'm stuck with this error:
/tmp/ccArHTZL.o: In function `main':
z.c:(.text+0x59): undefined reference to `cvLoadImage'
z.c:(.text+0xa0): undefined reference to `cvNamedWindow'
z.c:(.text+0xb1): undefined reference to `cvShowImage'
z.c:(.text+0xbb): undefined reference to `cvWaitKey'
z.c:(.text+0xc5): undefined reference to `cvDestroyWindow'
z.c:(.text+0xd1): undefined reference to `cvReleaseImage'
collect2: ld returned 1 exit status
In order to install OpenCV I've always followed this procedure:
$ sudo apt-get install libcv2.1 libcv-dev libcvaux2.1 libcvaux-dev libhighgui2.1
libhighgui-dev opencv-doc python-opencv
$ export LD_LIBRARY_PATH=/home/opencv/lib
$ export PKG_CONFIG_PATH=/home/opencv/lib/pkgconfig
$ pkg-config --cflags opencv
-I/usr/include/opencv
$ pkg-config --libs opencv
-lcxcore -lcv -lhighgui -lcvaux -lml
$ g++ -I/usr/include/opencv -lcxcore -lhighgui -lm hello.c
Anyone can help me?
Why don't you use pkg-config to your favor?
g++ hello.c -o hello `pkg-config --cflags --libs opencv`
I think it is because of some changes from gcc 4.5 to gcc 4.6
Try this command instead (i.e., move the libraries to the end, instead of at the beginning of your command line) -- it works for me:
g++ -I/usr/include/opencv hello.c -lcxcore -lhighgui -lm
I'm still on kubuntu 10.10 so I'm not really familiar how does 11.10 work, but the most common answer to problems with not finding libraries is to use ldconfig with sudo. It'll refresh libraries database. If that doesn't help, look into /usr/lib, /usr/lib64 and /usr/lib32, because its the default place where apt-get throws libraries in. When you find the libraries, change the LD_LIBRARY_PATH so it contains the directory. I don't think that /home/opencv/lib is where they are, but i don't know Your environment
I just upgraded to 11.04 on my laptop and having similar issues. I would try building the latest version of OpenCV (2.3.1) and see if this fixes anything, this seemed to fix quite a few issues for me.
Use the following command, it worked for me:
gcc pkg-config --cflags opencv opencv.c -o open_cv pkg-config --libs opencv