C++ program with gtk+ - c++

I want to call gtk+ routines from a C++ program test.cpp, and he encountered a problem while building my program.
I am getting the following error:
g++ test.cpp -o test.x
test.cpp:1:22: fatal error: gtk/gtk.h: No such file or directory
# include <gtk/gtk.h>
Somehow I need to link to gtk+. What will be the correct compiler declarations get my program working.

To compile a program using gtkmm, you need to use the following command:
g++ program.cc -o program `pkg-config --cflags --libs gtkmm-3.0`
The pkg-config --cflags --libs gtkmm-3.0 part provides the include paths and the linker options.
Source: The Flaming Manual, which you should Read.

I'm not sure about gtk in particular but this is a simple file not found issue.
You need to be sure that your system includes search path contains a folder gtk in it which in turn has a file called gtk.h.
Please read about the GCC search paths.
This is also likely a duplicated question so it will probably be closed off soon by the community.

Related

gcc: "cannot find -lasound" when compiling on Windows

I am compiling a C++ program on Windows. The program uses the SDL2 library. I run the following pkg-config command to obtain the correct flags to pass to the GCC compiler.
$ pkg-config sdl2 --cflags --libs
which gives output
-lpthread -lasound -IC:/sdl2/include/SDL2 -LC:/sdl2/lib -lSDL2
However when using these flags with GCC the program fails to compile, giving the error
cannot find -lasound
Is this a library I need to obtain in order to use SDL2, or am I making a mistake somewhere?
GCC not find this library. Find file libcomdlg32.a and find your -lasound file and copy -lasound file in folder, where's libcomdlg32.a file.

Cross compilation of C++ program which includes OpenCV header files fails

I can cross compile a simple C++ program. (ex. g++ -o test test.cpp & arm-linux-gnueabihf-g++ -o test test.cpp).
But when I include OpenCV header files in a C++ program and cross compile it, it throws an error.(ex. arm-linux-gnueabihf-g++ -o ocv_test ocv_test.cpp $(pkg-config --libs --cflags opencv)).
Also I can compile successfully with the default compiler i.e. g++.(ex. g++ -o ocv_test ocv_test.cpp $(pkg-config --libs --cflags opencv))
I executed cmake with
-D CMAKE_TOOLCHAIN_FILE=../platforms/linux/arm.toolchain.cmake \.
as per the opencv docs. That didn't help.
I added opencv include dir in compilation cmd ie -I/path/to/opencv/include and removing pkg-config. That didn't help.
I tried changing arm-gnueabi.toolchain.cmake by exporting PKG_CONFIG_LIBDIR as per suggestion by someone. That didn't help.
I got the following error:
__/usr/lib/gcc-cross/arm-linux-gnueabihf/7/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lopencv_shape
//usr/local/lib/libopencv_stitching.so: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status__
I want my cross-compiled OpenCV app to run on a RaspberryPI.
I'm confused. Cross compiling OpenCV sources with the -D CMAKE_TOOLCHAIN_FILE option should allow us to cross-compile OpenCV C++ programs. Otherwise we will have to cmake or make (cross-compile) every OpenCV C++ program seperately on a build machine.

Can't link GLFW library with it's header file on Ubuntu 18.04

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`

Trouble with compiling .cc files to be used in Intel Hyperscan

I am a student currently working on a project to implement Intel Hyperscan for Virus Signature Scanning on Ubuntu 16.04.
On the system, I have gcc 5.4.0 and g++ 5.4.0.
Hyperscan uses CMake to "build" itself.
Having successfully build Hyperscan, I can't seem to be able to find a way to compile my own code for it. I also could not find any "proper" way of compiling code meant for Hyperscan online, hence I assumed g++ would be right. I have tried,
g++ -o -std=c++11 test test.cc $(pkg-config --cflags --libs libhs)
only for it to give the error: hs.h: No such file or directory
Hence I used,
g++ -o -std=c++11 test test.cc -I../hyperscan/src $(pkg-config --cflags --libs
libhs)
to get another error:
/usr/bin/ld: cannot find -lhs and collect2: error: ld returned 1 exit status
At this point, I am kind of lost and can't really progress with my project. Am I using g++ wrongly? Have I built Hyperscan incorrectly? Any tips or solutions would be greatly appreciated!
$(pkg-config --cflags --libs libhs) only works when library and related development files are installed on the system. Looks like you have source code of library, so first you need to build and install library itself, only then compile your test program.
Also, library may be available in standard repositories, then you maybe want to install it using something like "sudo apt install libhs-dev", however, if you have source code of particular version that you need to work with, or library is closed source and non-public, then the only way is to compile and install it manually. Refer to documentation, README, INSTALL files that may be supplied with library code and follow build and installation procedures described there.

C++ Allegro5 CodeBlocks Linker

I'm running Ubuntu. I followed each and every step in the http://wiki.allegro.cc for installation and set up of Allegro5. If I run my program from the command line, I know I need to use
gcc -Wall main.cpp `pkg-config --cflags --libs allegro-5.0
plus any other packges I use. (I don't know what all of it means, but I know I need it)
What I need help with if figuring out what I need to do in Codeblocks > Settings > Compiler so that it will link to the allegro library so that I don't get a hundred and one undefined reference errors. I don't know what I'm looking for, and I don't know where to look. Help a new guy out.
Thanks.
edit: I know I'd need
allegro-config --libs --static
in the Linker for Allegro 4.2
Where can I look and what do I need for Allegro5?
If Codeblocks doesn't support entering `pkg-config --cflags --libs allegro-5.0` directly, then just open up a terminal and type in (no backticks):
pkg-config --cflags --libs allegro-5.0
Then copy/paste the result of that into the compiler settings inside Codeblocks.