Using Graphviz with Clion in Windows - c

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).

Related

Undefined references when included GLIB in clion using cmake

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

Linking SDL2 with CMake inside of CLion in Linux

I've been effortlessly trying to link SDL2 library to my CLion project. I've downloaded the FindSDL2 script, but I'm still getting undefined reference to an SDL function.
Here's my CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${project_SOURCE_DIR}/cmake")
project(SDL_Test C)
set(CMAKE_C_STANDARD 99)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(SDL_Test main.c)
target_link_libraries(SDL_Test ${SDL2_LIBRARY})
After trying to invoke SDL_Init(SDL_INIT_VIDEO) function I'm getting an undefined reference error:
[50%] Building C object CMakeFiles/SDL_Test.dir/main.c.o
[100%] Linking C executable SDL_Test
/bin/ld: CMakeFiles/SDL_Test.dir/main.c.o: in function `main':
/home/bartek/CLionProjects/SDL_Test/main.c:5: undefined reference to `SDL_Init'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/SDL_Test.dir/build.make:84: SDL_Test] Error 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/SDL_Test.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/SDL_Test.dir/rule] Error 2
make: *** [Makefile:118: SDL_Test] Error 2

Error while compiling with cmake: cannot find -ldwmapi

Edit: Im using MinGW-x86_64 on windows
When Im trying to compile a gtk+ 3 c project with cmake, I have this error message on the log:
"C:\Program Files\JetBrains\CLion 2017.1.3\bin\cmake\bin\cmake.exe" --build C:\Users\Jonas\ClionProjects\tutorial\cmake-build-debug --target tutorial -- -j 4
[ 50%] Linking C executable tutorial.exe
c:/mingw/bin/../lib/gcc/mingw32/4.9.3/../../../../mingw32/bin/ld.exe: cannot find -ldwmapi
collect2.exe: error: ld returned 1 exit status
CMakeFiles\tutorial.dir\build.make:96: recipe for target 'tutorial.exe' failed
mingw32-make.exe[3]: *** [tutorial.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/tutorial.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/tutorial.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/tutorial.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/tutorial.dir/rule] Error 2
mingw32-make.exe: *** [tutorial] Error 2
Makefile:117: recipe for target 'tutorial' failed
This is my CMakeLists.txt, in there I added gtk3 and its libraries, but when Im trying to compile, it has the errors mentioned before.
cmake_minimum_required(VERSION 3.7)
project(tutorial)
set(CMAKE_C_STANDARD 99)
set(SOURCE_FILES main.c)
set(PKG_CONFIG_EXECUTABLE "C:/msys64/mingw64/bin/pkg-config.exe")
FIND_PACKAGE(PkgConfig REQUIRED)
PKG_CHECK_MODULES(GTK3 REQUIRED gtk+-3.0)
INCLUDE_DIRECTORIES(${GTK3_INCLUDE_DIRS})
LINK_DIRECTORIES(${GTK3_LIBRARY_DIRS})
add_executable(tutorial ${SOURCE_FILES})
ADD_DEFINITIONS(${GTK3_CFLAGS_OTHER})
TARGET_LINK_LIBRARIES(tutorial ${GTK3_LIBRARIES})
dwmapi.dll is provided only for Vista. So I guess that it is a bug of package of gtk+-3.0. And -ldwmapi is not required for your OS. Below is a workaround to fix this issue. I don't make sure this will solve your issue. Note that this is self-responsibility.
make backup-copy of gdk-3.0.pc, gdk-broadway-3.0.pc, gdk-win32-3.0.pc in C:\msys64\mingw64\lib\pkgconfig
open those files in vim, remove -ldwmapi and :wq
UPDATE
Let's create libdwmapi.a
download def file from here
dlltool -d dwmapi.def -l libdwmapi.a

Using cmake with a shared (dynamic) library

I'm trying to use a simple shared library that I made with a file that just contains a main method.
I first ran cmake . which worked fine and didn't return any errors.
Then I ran make but got this error:
$ make
Scanning dependencies of target myprog
[ 50%] Building C object CMakeFiles/myprog.dir/main.c.o
[100%] Linking C executable myprog.exe
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lhello-user
collect2: error: ld returned 1 exit status
clang-3.8: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/myprog.dir/build.make:95: myprog.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/myprog.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
The CMakeLists.txt file
cmake_minimum_required(VERSION 2.8.8)
project(LIB_EXAMPLE)
set(CMAKE_C_COMPILER clang)
add_executable(myprog main.c)
target_link_libraries(myprog hello-user)
The library exists inside of /usr/local/lib/ as libhello-user.dll.a
Note: Im using Cygwin for cmake and make
Turning my comment into an answer
See CMake/Tutorials/Exporting and Importing Targets.
You either have:
to name a full path for the library
CMake is not searching for it automatically
you would have to add something like find_library(_lib_path NAMES hello-user)
or - better - put those into an IMPORTED target
cmake_minimum_required(VERSION 2.8.8)
project(LIB_EXAMPLE)
add_library(hello-user SHARED IMPORTED GLOBAL)
set_target_properties(
hello-user
PROPERTIES
IMPORTED_LOCATION /usr/local/lib/libhello-user.dll
IMPORTED_IMPLIB /usr/local/lib/libhello-user.dll.a
)
add_executable(myprog main.c)
target_link_libraries(myprog hello-user)

How do I fix this cmake file? c file is ignoring h file it is dependent on

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})

Resources