1

I installed graphics.h library on my Ubuntu 19.04, and when I write some code in my CLion IDE, it successfully includes the graphics.h Header file, and also after writing some basic functions, the IDE doesn't show any errors, but when I compile the Code it gives Errors.

My Code:

#include <stdio.h>
#include <graphics.h>

int main() {

    int gd = DETECT, gm;
    detectgraph(&gd, &gm);
    initgraph(&gd, &gm, NULL);

    line(10, 10, 50, 50);

    closegraph();

}

My CMakeList.txt File:

cmake_minimum_required(VERSION 3.14)
project(CGR_MP C)

set(CMAKE_C_STANDARD 11)

include_directories("//usr//local//lib//libgraph-1.0.2//")

build_command("lgraph")

add_executable(CGR_MP main.c)

Errors:

====================[ Build | CGR_MP | Debug ]==================================
/snap/clion/73/bin/cmake/linux/bin/cmake --build /home/prathamesh/Desktop/College/Sem-2/CGR-MP/cmake-build-debug --target CGR_MP -- -j 2
Scanning dependencies of target CGR_MP
[ 50%] Building C object CMakeFiles/CGR_MP.dir/main.c.o
[100%] Linking C executable CGR_MP
/usr/bin/ld: CMakeFiles/CGR_MP.dir/main.c.o: in function `main':
/home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:7: undefined reference to `detectgraph'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:8: undefined reference to `initgraph'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:10: undefined reference to `line'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:12: undefined reference to `closegraph'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/CGR_MP.dir/build.make:84: CGR_MP] Error 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/CGR_MP.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/CGR_MP.dir/rule] Error 2
make: *** [Makefile:118: CGR_MP] Error 2

Despite everything being successfully installed, it shows all Graphics functions as undefined reference

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • You need to tell `cmake` to link the graphics library too. – Jonathan Leffler Aug 10 '19 at 14:34
  • I need to use graphics.h for my School Project, and Jonathan how do I tell cmake to link graphics library? – Prathamesh Mutkure Aug 10 '19 at 16:47
  • Read up on compilation, linking and libraries. [This](https://www.cs.utah.edu/~zachary/isp/tutorials/separate/separate.html) will do. The CMake directive to link library is [target_link_libraries](https://cmake.org/cmake/help/latest/command/target_link_libraries.html)`(your-program library1 library2 ...)`. Have a great success in your endeavour! – Victor Sergienko Aug 14 '19 at 20:42
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Victor Sergienko Aug 14 '19 at 20:44

0 Answers0