Hi there,
I’m a student trying to use Graphviz as an external library in my C project, using Windows. However, when I try to compile it, I receive undefined reference errors to the functions included in “gvc.h” and “cgraph.h”. For example, agread and gvContext.
Currently, I am only working on the Windows portion. Here is my Makefile:
.PHONY: all clean
#CFLAGS = `pkg-config --cflags glib-2.0`
#LDLIBS = `pkg-config --libs glib-2.0`
CFLAGS = -I"C:/Program Files/Graphviz/include/graphviz"
LDIR = -L"C:/Program Files/Graphviz/lib"
LIBS = -lgvc -lcgraph
binaries=sqlpsql
all: $(binaries)
sqlpsql: SQLP.c SQLPGrammar.y SQLPScanner.l SQLPtoSQL-main.c Preprocess.c Rules.c
ifeq ($(OS),Windows_NT)
win_bison -v -d SQLPGrammar.y
win_flex --nounput -D SQLPGrammar SQLPScanner.l
gcc -Wall -o sqlpsql SQLP.c SQLPtoSQL-main.c Preprocess.c Rules.c $(CFLAGS) $(LDIR) $(LIBS)
else
bison -v -d SQLPGrammar.y
flex --nounput -D SQLPGrammar SQLPScanner.l
gcc -Wall SQLP.c SQLPtoSQL-main.c Preprocess.c Rules.c -o sqlpsql
rm -f lex.yy.c SQLPGrammar.tab.c SQLPGrammar.tab.h
endif
clean:
-rm -f *.o *.output $(binaries)
On my computer, the header files are in: C:/Program Files/Graphviz/include/graphviz
The lib files are in: C:/Program Files/Graphviz/lib
I have added both locations to the Makefile using the -I and -L tags, as above.
Along with a very simple main function invoking the two functions (in another file), here is my SQLP.c file:
#include <stdio.h>
#include <gvc.h>
#include <cgraph.h>
#include "SQLP.h"
void generate_graph(){
FILE *fp;
fp = fopen("C:/Users/claud/Documents/UW/4A/Roseseed/test.dot", "w+");
fputs("digraph ERD {\n", fp);
fputs("graph [ rankdir = \"LR\" ];\n", fp);
fputs("ranksep=2;\n", fp);
fputs("\"SCOTT.DEPT\" [ label=\"<SCOTT.DEPT> SCOTT.DEPT|<PK_DEPT>DEPTNO \\l |DNAME \\l LOC \\l \" shape = \"record\" ];\n", fp);
fputs("\"SCOTT.EMP\" [ label=\"<SCOTT.EMP> SCOTT.EMP|<FK_DEPTNO>DEPTNO \\l |EMPNO \\l ENAME \\l JOB \\l MGR \\l STARTDATE \\l SAL \\l COMM \\l \" shape = \"record\" ];\n", fp);
fputs("\"SCOTT.DEPT\":\"PK_DEPT\"->\"SCOTT.EMP\":\"FK_DEPTNO\" [arrowhead = crow];}\n", fp);
fclose(fp);
export_graph();
}
void export_graph(){
FILE *fp;
fp = fopen("C:/Users/claud/Documents/UW/4A/Roseseed/test.dot", "r");
Agraph_t *g;
g = agread(fp, 0);
GVC_t *gvc;
gvc = gvContext();
gvLayout(gvc, g, "dot");
gvRender(gvc, g, "png",
fopen("C:/Users/claud/Documents/UW/4A/Roseseed/test.png", "w"));
gvFreeLayout(gvc, g);
agclose(g);
}
All other files compile properly because they do not include these header files.
And here is the full output of running make on Git Bash in the location of the Makefile:
$ make
win_bison -v -d SQLPGrammar.y
win_flex --nounput -D SQLPGrammar SQLPScanner.l
gcc -Wall -o sqlpsql SQLP.c SQLPtoSQL-main.c Preprocess.c Rules.c -I"C:/Program Files/Graphviz/include/graphviz" -L"C:/Program Files/Graphviz/lib" -lgvc -lcgraph
C:\Users\claud\AppData\Local\Temp\ccEwgb5N.o:SQLP.c:(.text+0x3abe): undefined reference to `_imp__agread'
C:\Users\claud\AppData\Local\Temp\ccEwgb5N.o:SQLP.c:(.text+0x3ac8): undefined reference to `_imp__gvContext'
C:\Users\claud\AppData\Local\Temp\ccEwgb5N.o:SQLP.c:(.text+0x3ae7): undefined reference to `_imp__gvLayout'
C:\Users\claud\AppData\Local\Temp\ccEwgb5N.o:SQLP.c:(.text+0x3b1b): undefined reference to `_imp__gvRender'
C:\Users\claud\AppData\Local\Temp\ccEwgb5N.o:SQLP.c:(.text+0x3b2f): undefined reference to `_imp__gvFreeLayout'
C:\Users\claud\AppData\Local\Temp\ccEwgb5N.o:SQLP.c:(.text+0x3b3c): undefined reference to `_imp__agclose'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:13: sqlpsql] Error 1
It seems like the header files are found, just none of the functions are defined. What can I do to fix this and compile the project? I am a beginner, any help is greatly appreciated!