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.
Related
I am trying to use the GSL library with C++ in Xcode and I was having an issue linking to the library.
I am able to link to GSL using the terminal and clang with the following linker flags
clang++ -std=c++17 -Wall -pedantic test.cpp -o test -lgsl -lgslcblas
From the command line I'm able to verify it uses the GSL library for the computation. However in Xcode, I get a "'gsl/gsl_linalg.h' file not found" error when I try to build it. Its called in my header file with
#include <gsl/gsl_linalg.h>
I added -lgsl -lgslcblas into Xcode build settings thru the "Other Linker flags" option.
I also ran
gsl-config --cflags
gsl-config --libs
and got the following output
-I/usr/local/include
-L/usr/local/lib -lgsl -lgslcblas
So it seems like GSL is installed properly and can be used by clang but for some reason when I use clang through Xcode it isn't able to use it. I was also able to manually find that particular header file in /usr/local/include/gsl/gsl_linalg.h
Any ideas on what I'm might be doing wrong?
I am trying to link GLFW to my C program.
The docs seem to suggest #include<GLFW/glfw3.h> however I have installed 2.7.2 (from my distro's repository) and don't have that header file:
find / -name *glfw* 2> /dev/null
/usr/lib/libglfw.so.2.6
/usr/lib/libglfw.a
/usr/lib/libglfw.so
/usr/lib/pkgconfig/libglfw.pc
/usr/lib/libglfw.so.2
/usr/include/GL/glfw.h
/usr/share/doc/libglfw-dev
/usr/share/doc/libglfw2
/var/cache/apt/archives/libglfw2_2.7.2-1_i386.deb
/var/cache/apt/archives/libglfw-dev_2.7.2-1_i386.deb
/var/lib/dpkg/info/libglfw2.list
/var/lib/dpkg/info/libglfw2.postinst
/var/lib/dpkg/info/libglfw-dev.md5sums
/var/lib/dpkg/info/libglfw2.postrm
/var/lib/dpkg/info/libglfw2.md5sums
/var/lib/dpkg/info/libglfw2.shlibs
/var/lib/dpkg/info/libglfw-dev.list
I tried #include<GL/glfw.h> but I still get undefined reference to 'glfwLoadTexture2D'
How do I link to GLFW and use glfwLoadTexture2D()?
An #include does nothing for the linker; it just brings in declarations, not the actual functions.
The documentation indicates that GLFW uses pkg-config (not surprising; #elmindreda knows her stuff), so your compilation line should be something like:
$ cc `pkg-config --cflags glfw3` -o foo foo.c `pkg-config --static --libs glfw3`
Also note that since the library uses pkg-config, you're not supposed to "care" about details such as where the header and library files are located on your particular installation. Just ask using the --cflags and --libs modes, and you will get the proper locations returned, as the example above indicates.
You are mixing up compilation and linking. If you were missing headers, you would probably have errors a lot sooner than the linking stage.
"Undefined reference" results from symbols not being found by the linker. The most likely cause is you not telling gcc that it should link to the GLFW libraries:
gcc myfile.c -lglfw
When I am on Linux, I compile opengl/glfw projects like this:
gcc main.c -lGL -lglfw
When I am on windows, I compile them by writing:
gcc main.c libglfw3.a -lopengl32 -lgdi32
and I put libglfw3.a file in the same directory where main.c is. I have read people say that they couldn't link properly before writing
-lopengl32 -lgdi32 -luser32 -lkernel32 -lws2_32.
Another thing which may be worth mentioning is that I couldn't link glfw libraries when I downloaded 32bit glfw binaries. When I downloaded 64bit glfw binaries everything worked fine. I have a 64 bit machine and a x86_64-w64-mingw32. I have read comments from people with the opposite experience, where they weren't able to link glfw libraries when they downloaded 64bit binaries, but they were able to link them after downloading 32bit binaries. My advice would be to try both.
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.
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 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.