By some reason the following do now work
/usr/bin/c++ -lm -L/usr/X11R6/lib -lX11 -lpthread CMakeFiles/net.dir/advanced.cpp.o -o net -rdynamic
But this do:
/usr/bin/c++ -lm -L/usr/X11R6/lib CMakeFiles/net.dir/advanced.cpp.o -lX11 -lpthread -o net -rdynamic
My question is how to force CMake to put option AFTER /advanced.cpp.o to make compilation possible in my case. All CMake options I used put my custom GCC option before compiler.
My cmake file
cmake_minimum_required (VERSION 2.6)
project (mnist)
set( CMAKE_VERBOSE_MAKEFILE on )
add_compile_options("-O2")
add_compile_options("-std=c++11")
SET( CMAKE_EXE_LINKER_FLAGS "-L/usr/X11R6/lib -lm -lpthread -lX11")
include_directories("${PROJECT_SOURCE_DIR}")
add_executable(net advanced.cpp)
Use target_link_libraries instead of adding CMAKE_EXE_LINKER_FLAGS.
And use link_directories instead of -L option in linker flags.
Related
I would like to CGO link my go program to a c++ static library linked to LLVM's libc++. With GNU's libstdc++, everything works fine.
I am calling go compiler as follows:
CGO_CXXFLAGS="-stdlib=libc++ -I/usr/lib/clang/12.0.0/include -I/usr/local/include -I/usr/include" CGO_LDFLAGS="-stdlib=libc++ -L/usr/lib/clang/12.0.0/lib -L/usr/local/lib -L/usr/lib -L/usr/lib/x86_64-linux-gnu -lpthread -lrt -lc++ -lm -lc" go test --ldflags '-extldflags "-static"' -c -a .
I was missing -lc++ -lc++abi.
I use cmake to create my project using ffmpeg lib, the project is simple.
add_executable(testffmpeg main.cpp)
link_directories( /usr/lib/x86_64-linux-gnu )
target_link_libraries(testffmpeg libavcodec.a libavutil.a )
When make, there's lots of undefined reference errors (my default cxx compiler is clang by "export cxx=/usr/bin/clang++").
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libavcodec.a(crystalhd.o): In function `receive_frame':(.text+0xef): undefined reference to `DtsProcOutputNoCopy'
but when I just use command, it's OK.
clang -o testffmpeg -lavutil -lavcodec main.cpp
while using gcc also get this error
gcc -o testffmpeg -lavutil -lavcodec main.cpp
so, how can I resolve this problem and where is the mistake?
You need add -lcrystalhd in your linker flags.
You can use code below to find all linker flags.
pkg-config --libs libavformat
-L/usr/local/lib -lavformat -lXv -lX11 -lXext -ldl -lvdpau -lva -lva-x11 -lX11 -lva -lva-drm -lva -lxcb -lxcb-shm -lxcb -lxcb-xfixes -lxcb-render -lxcb-shape -lxcb -lxcb-shape -lxcb -lsndio -ljack -lasound -lSDL2 -lx264 -lcrystalhd -lm -llzma -lbz2 -lz -pthread -lavcodec -lXv -lX11 -lXext -ldl -lvdpau -lva -lva-x11 -lX11 -lva -lva-drm -lva -lxcb -lxcb-shm -lxcb -lxcb-xfixes -lxcb-render -lxcb-shape -lxcb -lxcb-shape -lxcb -lsndio -ljack -lasound -lSDL2 -lx264 -lcrystalhd -lm -llzma -lbz2 -lz -pthread -lswresample -lm -lavutil -lm
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I actualy work on a openGL projet on my VM but i need openGL 4.5 so i have install ubuntu on my laptop with (GTX 870M) who is compatible 4.5 (i check with glxinfo). But my problem is after install gcc, build-essential, libglew-dev, freeglut3-dev, freeglut3 and SDL2. I can't make my projet i have error like undefined reference on « SDL_WasInit » , « glBegin », .... for all library installed...
i try with makefile like :
ifeq "$(shell uname)" "Darwin"
LIBGL= -framework OpenGL
else
LIBGL= -lGLU -lGL
endif
CXXFLAGS += `pkg-config glew --cflags` `sdl2-config --cflags` -g -W -Wall -Wno-unused-parameter -Wno-deprecated-declarations
LDFLAGS += `pkg-config glew --libs` `sdl2-config --libs` $(LIBGL)
all : main.exe
run : main.exe
./main.exe
main.exe : main.cpp *.h
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o$# main.cpp
sol : main_solution.exe
runs : main_solution.exe
./main_solution.exe
main_solution.exe : main_solution.cpp *.h
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $# main_solution.cpp
clean :
rm -f *.o *.exe
and with cmakefile like :
cmake_minimum_required(VERSION 3.3)
project(src)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lglut -lGLU -lGL -lGLEW -lm -lSDL2 -lSDL2main -Wall -g")
set(SOURCE_FILES
"""ALL SOURCE FILES"""")
add_executable(src ${SOURCE_FILES})
This projet (cmakefile and makefile) work fine on my virtual machine ...
I hope you can help me thx.
In CMake you use target_link_libraries to specify which libraries to use. In general you want to use find_package(…) to locate the configuration for a specific library, then use the variables introduced by it to link.
find_package(OpenGL)
add_executable(foo …)
target_link_libraries(foo ${OPENGL_gl_LIBRARY} …)
You should look into the cmake modules that are used by find_package to see what the names of the variables they configure are.
So I was merrily learning GTK+ programming with 3.0 in Ubuntu 11.04, and even got a working ruler program running. It compiled with my makefile perfectly.
I took some time away from it, upgraded to 11.10, and now have come back to it. But, alas, when I try to build the program, I get a lot of "undefined reference" errors to pretty much every gtk call in my program. Compiling is fine - the issue is at link time.
The project can be found here: https://github.com/zjmichen/ruler
I've tested it and it compiled fine.
superman#superman-mint ~/work/zjmichen-ruler-e783fe1 $ make
gcc -c -Wall `pkg-config --cflags --libs gtk+-3.0` main.c
gcc -c -Wall `pkg-config --cflags --libs gtk+-3.0` window.c
gcc -c -Wall `pkg-config --cflags --libs gtk+-3.0` graphics.c
gcc -c -Wall `pkg-config --cflags --libs gtk+-3.0` mouse.c
gcc `pkg-config --cflags --libs gtk+-3.0` main.o window.o graphics.o mouse.o -o zruler
superman#superman-mint ~/work/zjmichen-ruler-e783fe1 $
But this was on Linux Mint. Here you've got the arguments produced by pkg-config
-pthread -DGSEAL_ENABLE -I/usr/include/atk-1.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/gtk-3.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pixman-1 -pthread -L/usr/lib/x86_64-linux-gnu -lgtk-3 -lgdk-3 -latk-1.0 -lcairo-gobject -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lm -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lrt -lglib-2.0
I've also did a quick test on Ubuntu 11.10 and indeed it doesn't compile. I'll take a better look this evening.
So I did test it on Ubuntu and changed the line 14 in the makefile to
$(CC) $(OBJS) $(GTKFLAGS) -o $(NAME)
this changes the order of object files and libraries that are being linked. I have no idea why this problem occurs on the new Ubuntu. Maybe it is because it is a different version of gcc. On my linux mint gcc is version 4.5.2 on ubuntu 11.10 it is 4.6.1
Unfortunately I currently don't have access to a running Linux machine with GTK3.0 installed, but looking at your code I noticed the inclusion of X11/xlib.h in main.c. Since you are not using X11 code anywhere in your code and you are using 'pkgconfig ... gtk+-3.0' instead of gtk+-X11-3.0, I assume you could remove that include statement. If you do need the X11 libraries the make sure it is listed in the compiler flags. I hope this helps :)
Under gcc (g++), I have compiled a static .a (call it some_static_lib.a) library. I want to link (is that the right phrase?) this .a file into another dynamic library (call it libsomeDyn.so) that I'm building. Though the .so compiles, I don't see content of .a under .so using nm command:
/usr/bin/g++ -fPIC -g -O2 -Wall -Werror -pipe -march=pentium3
-mtune=prescott -MD -D_FILE_OFFSET_BITS=64 -DLINUX -D_GNU_SOURCE -D_THREAD_SAFE -I../../../../../../../../ -I../../../../../../../..//libraries -Wl,-rpath,/usr/lib -o libsomeDyn.so some.o another.o some_static_lib.a -shared -Wl -x
-Wl,-soname,libsomeDyn.so
I do not see functions under some_static_lib.a under libsomeDyn.so. What am I doing wrong?
Static libraries have special rules when it comes to linking. An object from the static library will only be added to the binary if the object provides an unresolved symbol.
On Linux, you can change that behavior with the --whole-archive linker option:
g++ -Wl,--whole-archive some_static_lib.a -Wl,--no-whole-archive
For every one that comes across that problem like me (and has not understand the answer properly): here is a short howto generate a dynamic library (libmylib.so) from a static one (mylib.a):
1.) create a mylib.c file that only imports the mylib.h file
2.) compile this mylib.c to mylib.o with
gcc -c -fPIC mylib.c -o mylib.o
3.) generate a dynamic library with the following command:
gcc --whole-archive -shared -Wl,-soname,libmylib.so -o libmylib.so mylib.o mylib.a
That worked at least for me, turning a static library (compiled with -fPIC) to
a dynamic library. I'm not sure wether this will work for other libraries too.