I am trying to write a notification service for gnome but cmake keeps outputting undefined reference errors when building. I am using clion IDE and glib2.0 libraries and running on Fedora workstation 30.
What I have tried so far is searching lots of stack overflow threads and googling for hours.
My CMakeLists.txt file, which is copied from another StackOverflow thread, is here:
cmake_minimum_required(VERSION 3.14)
project(gnome_notification C)
set(CMAKE_C_STANDARD 99)
include(FindPkgConfig)
pkg_check_modules(GLIB glib-2.0 REQUIRED)
include_directories(${GLIB_INCLUDE_DIRS})
set(SOURCE_FILES main.c)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${GLIB_LIBRARIES})
My main.c file is here:
#include <stdio.h>
#include <gio/gio.h>
int main() {
GApplication* application = g_application_new ("Test", G_APPLICATION_FLAGS_NONE);
GNotification* notification = g_notification_new("Test Title");
g_notification_set_body(notification, "Test body");
g_notification_set_priority(notification, G_NOTIFICATION_PRIORITY_NORMAL);
g_application_send_notification(application, "Test Id", notification);
return 0;
}
The build yields undefined reference error:
/home/xgao/bin/Clion/bin/cmake/linux/bin/cmake --build /home/xgao/Desktop/gnome_notification/cmake-build-debug --target gnome_notification -- -j 4
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xgao/Desktop/gnome_notification/cmake-build-debug
[ 50%] Linking C executable gnome_notification
/usr/bin/ld: CMakeFiles/gnome_notification.dir/main.c.o: in function `main':
/home/xgao/Desktop/gnome_notification/main.c:4: undefined reference to `g_application_new'
/usr/bin/ld: /home/xgao/Desktop/gnome_notification/main.c:5: undefined reference to `g_notification_new'
/usr/bin/ld: /home/xgao/Desktop/gnome_notification/main.c:6: undefined reference to `g_notification_set_body'
/usr/bin/ld: /home/xgao/Desktop/gnome_notification/main.c:7: undefined reference to `g_notification_set_priority'
/usr/bin/ld: /home/xgao/Desktop/gnome_notification/main.c:8: undefined reference to `g_application_send_notification'
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/gnome_notification.dir/build.make:84: gnome_notification] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/gnome_notification.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/gnome_notification.dir/rule] Error 2
gmake: *** [Makefile:118: gnome_notification] Error 2
You need something like
pkg_check_modules(GLIB glib-2.0 gio-2.0 REQUIRED)
or additionally
pkg_check_modules(GIO gio-2.0 REQUIRED)
because GNotification is provided by libgio, which is a separate library from libglib, even though they both come from the same source tree.
Try this:
gcc -o note `pkg-config --cflags --libs gio-2.0` note.c
Related
I'm new to using external libraries in C so this could be a very silly mistake. I get reference errors when I try to run the below program using the provided CMakeLists.txt. Can anyone see what the issue is?
CMAKELists.txt
cmake_minimum_required(VERSION 3.6)
project(Learning)
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}")
set(GRAPHVIZ_INCLUDE_DIR "C:\\Program Files (x86)\\Graphviz2.38\\include\\graphviz")
set(SOURCE_FILES main.c)
include_directories("${GRAPHVIZ_INCLUDE_DIR}")
add_executable(Learning ${SOURCE_FILES})
main.c
#include <gvc.h>
#include <cgraph.h>
int main() {
Agraph_t *graph;
Agnode_t *nodeA, *nodeB;
Agedge_t *edge1;
Agsym_t *symbol1;
GVC_t *gvc;
gvc = gvContext();
graph = agopen( "graph", Agdirected, NULL);
nodeA = agnode(graph, "nodeA", 1);
nodeB = agnode(graph, "nodeB", 1);
edge1 = agedge(graph, nodeA, nodeB, 0, 1);
agsafeset(nodeA, "color", "red", "");
gvLayoutJobs(gvc, graph);
gvRenderJobs(gvc, graph);
gvFreeLayout(gvc, graph);
}
output
"C:\Program Files\JetBrains\CLion 2017.2.1\bin\cmake\bin\cmake.exe" --build E:\Development\C\Learning\cmake-build-debug --target Learning -- -j 2
Scanning dependencies of target Learning
[ 50%] Building C object CMakeFiles/Learning.dir/main.c.obj
[100%] Linking C executable Learning.exe
CMakeFiles\Learning.dir/objects.a(main.c.obj): In function `main':
E:/Development/C/Learning/main.c:38: undefined reference to `gvContext'
E:/Development/C/Learning/main.c:39: undefined reference to `_imp__Agdirected'
E:/Development/C/Learning/main.c:39: undefined reference to `agopen'
E:/Development/C/Learning/main.c:40: undefined reference to `agnode'
E:/Development/C/Learning/main.c:41: undefined reference to `agnode'
E:/Development/C/Learning/main.c:42: undefined reference to `agedge'
E:/Development/C/Learning/main.c:44: undefined reference to `agsafeset'
E:/Development/C/Learning/main.c:45: undefined reference to `gvLayoutJobs'
E:/Development/C/Learning/main.c:46: undefined reference to `gvRenderJobs'
E:/Development/C/Learning/main.c:47: undefined reference to `gvFreeLayout'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Learning.exe] Error 1
CMakeFiles\Learning.dir\build.make:96: recipe for target 'Learning.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/Learning.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Learning.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Learning.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/Learning.dir/rule] Error 2
mingw32-make.exe: *** [Learning] Error 2
Makefile:117: recipe for target 'Learning' failed
Edit -- edited cmakeslists to add target_link_libraries but then I get the error below
cmake_minimum_required(VERSION 3.6)
project(Learning)
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}")
set(GRAPHVIZ_INCLUDE_DIR "C:\\Program Files (x86)\\Graphviz2.38\\include\\graphviz")
set(GRAPHVIZ_LIB_DIR "C:\\Program Files (x86)\\Graphviz2.38\\lib\\release\\lib")
target_link_libraries( "${GRAPHVIZ_LIB_DIR}" )
set(SOURCE_FILES main.c)
include_directories("${GRAPHVIZ_INCLUDE_DIR}")
add_executable(Learning ${SOURCE_FILES})
Results in this error
"C:\Program Files\JetBrains\CLion 2017.2.1\bin\cmake\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" E:\Development\C\Learning
CMake Error at CMakeLists.txt:8 (target_link_libraries):
Cannot specify link libraries for target "C:\Program Files
(x86)\Graphviz2.38\lib\release\lib" which is not built by this project.
-- Configuring incomplete, errors occurred!
See also "E:/Development/C/Learning/cmake-build-debug/CMakeFiles/CMakeOutput.log".
[Finished]
You need to link in graphviz library using target_link_libraries(Learning path/to/graphviz.so).
the cmake file
cmake_minimum_required(VERSION 3.6)
project(producer)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -pthread")
add_executable(server wrappers.h wrappers.c server.c request.h request.c)
the error message
`CMakeFiles/server.dir/wrappers.c.o: In function Pthread_create':
/home/user/ClionProjects/producer/wrappers.c:570: undefined reference to `pthread_create'
CMakeFiles/server.dir/wrappers.c.o: In function `Pthread_join':
/home/user/ClionProjects/producer/wrappers.c:645: undefined reference to `pthread_join'
CMakeFiles/server.dir/wrappers.c.o: In function `Pthread_cancel':
/home/user/ClionProjects/producer/wrappers.c:653: undefined reference to `pthread_cancel'
collect2: error: ld returned 1 exit status
make[3]: *** [server] Error 1
make[2]: *** [CMakeFiles/server.dir/all] Error 2
make[1]: *** [CMakeFiles/server.dir/rule] Error 2
make: *** [server] Error 2
wrapper.c includes wrapper.h which includes pthread.h
I'm overlooking something obvious, but I don't know what.
I tried set, add_library, and install commands but never got output with fewer errors than this.
Edit: Here's the fixed file.
cmake_minimum_required(VERSION 3.6)
project(producer)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -lpthread")
add_library(wrapper wrapper.h wrapper.c)
add_library(cs537 cs537.h cs537.c)
add_library(request request.h request.c)
add_executable(server server.c)
target_link_libraries(server request wrapper -lpthread)
thats a linker error, you need to use -pthread in the linker aswell, either directly:
target_link_libraries(server -lpthread)
or more cross-platform:
set(CMAKE_THREAD_PREFER_PTHREAD ON)
find_package (Threads)
target_link_libraries (server ${CMAKE_THREAD_LIBS_INIT})
I am trying to resurrect on old OpenGL program on a Linux Mint system. I installed freeglut3-dev using Synaptic Package Manager. The compiler does not complain about not being able to find the glut include file anymore, but now I have other problems:
cc -Wall -o gears main.c draw_gears.c gl_drawing.c load_data.c normal.c prep_data.c -lglut
/usr/bin/ld: /tmp/ccfNsT0O.o: undefined reference to symbol 'glNewList'
//usr/lib/i386-linux-gnu/mesa/libGL.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: *** [gears] Error 1
Try this:
cc -Wall -o gears main.c draw_gears.c gl_drawing.c load_data.c normal.c prep_data.c -lglut -lGLU -lGL
I am trying to learn and use SDL on my project. at first I had some problems where my ide can't find it. I'm using clion ide and mingw. I added the sdl on mingw (C:\MinGW\include\SDL2) and now it's working. But still I can't compile. any idea about this error?
Linking C executable Hello_World.exe
CMakeFiles\Hello_World.dir/objects.a(main.c.obj): In function `SDL_main':
C:/Users/Deve/ClionProjects/Hello World/main.c:5: undefined reference to `SDL_Init'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libmingw32.a(main.o):(.text.startup+0xa7): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
this my only code for now
#include "SDL2/SDL.h"
int main(int argc, char *argv[]){
SDL_Init(SDL_INIT_VIDEO);
return 0;
}
and I can't make it working.
here is my cmake
cmake_minimum_required(VERSION 3.2)
project(Hello_World)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror")
set(SOURCE_FILES main.c)
add_executable(Hello_World ${SOURCE_FILES})
my system is windows 8.1 64bit
Did you included .a and / or .lib ?
If you don't include that, your program will never find the functions you want to use.
You should include -lSDL flag in your cmake file:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -lSDL")
I'm trying to set up the code from
http://www.openglsuperbible.com/example-code/
with command:
$ cmake -G "Unix Makefiles" ; make clean ; make all
I've installed the following packages in order to prepare the system:
xrandr libxrandr x11-xserver-utils libxrandr-dev libglfw2 libglfw-dev xorg-dev libx11-dev libgl-dev libglu-dev libgl1-mesa-dev freeglut3 freeglut3-dev mesa-common-dev freeglut3-dev build-essential libx11-dev libx11-*
I may have missed some of the packages I've installed in this list, I apologize.
Then I've modified the CMakeList.txt in order to add to the linking phase the following things:
glfw GL rt glut X11 dl Xrandr Xext GLEW GLU
Then I try to compile, but it fails at the linking phase:
/opt/sb6code/src/xraw/xraw.c: In function ‘CreateWindow’:
/opt/sb6code/src/xraw/xraw.c:106:14: warning: assignment makes pointer from integer without a cast [enabled by default]
Linking C executable bin/xraw
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'XOpenDisplay'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'XCloseDisplay'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'XCreateColormap'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'XCreateWindow'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'XMapWindow'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'glXCreateContextAttribsARB'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function Cleanup: error: undefined reference to 'XDestroyWindow'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function Cleanup: error: undefined reference to 'XCloseDisplay'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function main: error: undefined reference to 'XNextEvent'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function main: error: undefined reference to 'XGetWindowAttributes'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/xraw] Errore 1
make[1]: *** [CMakeFiles/xraw.dir/all] Errore 2
make: *** [all] Errore 2
Now, I've tried to search the web and even here on SO.com for solutions, but they keep telling me to link the libraries I've already added to the linking phase.. so I can't seem to make any more progress.
Do you happen to have some knowledge or hint that could help me?
Ok, stimulated by answers I've gone into CMakeFiles directory and looked for the configuration file that manages the compilation. I've found out that for some reasons it did miss some of the flags stated into the CMakeLists despite of my update.
So now the compilation line looks like:
/usr/bin/cc CMakeFiles/xraw.dir/src/xraw/xraw.c.o -o bin/xraw -L/opt/sb6code/lib -rdynamic -lGL -lglut -lGLU -lm -lX11 -ldl -lGLEW -lXrandr -lXext -lrt -lglfw -Wl,-rpath,/opt/sb6code/lib
And the errors have reduced to :
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'glXCreateContextAttribsARB'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/xraw] Errore 1
make[1]: *** [CMakeFiles/xraw.dir/all] Errore 2
make: *** [all] Errore 2
Other hints? :)
Side Note:
I've also installed CUDA libraries and under path
/usr/lib/nvidia-304/
I happen to have
alt_ld.so.conf libnvidia-ml.so.1
bin/ libnvidia-ml.so.304.88
ld.so.conf libnvidia-opencl.so.1
libcuda.so libnvidia-opencl.so.304.88
libcuda.so.1 libnvidia-tls.so.304.88
libcuda.so.304.88 libnvidia-wfb.so.1
libGL.so libnvidia-wfb.so.304.88
libGL.so.1 libOpenCL.so
libGL.so.304.88 libOpenCL.so.1
libnvcuvid.so libOpenCL.so.1.0
libnvcuvid.so.1 libOpenCL.so.1.0.0
libnvcuvid.so.304.88 libXvMCNVIDIA_dynamic.so.1
libnvidia-cfg.so libXvMCNVIDIA.so
libnvidia-cfg.so.1 libXvMCNVIDIA.so.1
libnvidia-cfg.so.304.88 libXvMCNVIDIA.so.304.88
libnvidia-compiler.so tls/
libnvidia-compiler.so.1 vdpau/
libnvidia-compiler.so.304.88 xorg/
libnvidia-glcore.so.304.88 XvMCConfig
libnvidia-ml.so
Which I would like to use since I'm not sure that my intel graphic card supports OpenGl 4.* which I shall use with the book, while the nvidia one supports 4.2.
I really can't find any good how to on the matter, the Book conveniently avoids to give installation instructions.
Temporary Solution:
I've found out that this is the only source file that does not work. Removing its files and CMakeList.txt configuration seems to be wise until someone finds out another solution.
Also, i had to use optirun to run the executables, in case other people with bumblebee/nvidia are wondering why their programs do not work.
You probably link the libraries in wrong order; all these symbols are defined in libX11.a; you should want to see what is the actual linking command line fed to GCC, and post it here.
Update now it means that you are statically trying to link against an API function that is not available in your libs (it is an optional extension and should be queried dynamically).