Undefined reference to `_imp__agread'

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! :slight_smile:

The agread function is defined in lib/cgraph. Try using LIBS = -lgvc -lcgraph.

Thanks for your message, I tried that and I am still receiving the same error. Please let me know if there is anything else I could try!

Try removing -static

Thanks! Unfortunately, not that either. That was my attempt to remove the _ imp from the error message, which was not successful…

Can you post your full source code and the full Makefile? Also the command you’re invoking to build and the full output you get?

Yes, posted! Thank you and please let me know if there is anything else I can show you that would be helpful.

You write that you are on Windows. Are you using MinGW or Cygwin or something else that gives you make and gcc?

There were a lot of missing files, but after working around that I managed to compile and link your project without any problems. However, if I comment out the LIBS = -lgvc -lcgraph line in the Makefile I get exactly the same error messages like you. This suggests to me that the libraries actually are not in the location that you’ve given or that they have an unexpected name of format.

Try:

ls -l "C:/Program Files/Graphviz/lib"

and see what you find. If you are using MinGW you should see something like libcgraph.dll.a and libgvc.dll.a at least.

Now that I think of it, I think you’re more likely to see something like cgraph.lib and gvc.lib which also should work.

If you are not using MinGW, but Cygwin, you might want to try to download the experimental Cygwin version from https://gitlab.com/graphviz/graphviz/-/package_files/17473242/download, but I haven’t tried that myself.

This is what I see when I run that command:

$ ls -l "C:/Program Files/Graphviz/lib"
total 128
-rw-r--r-- 1 claud 197609  5674 Jan 18  2021 cdt.lib
-rw-r--r-- 1 claud 197609 20492 Jan 18  2021 cgraph.lib
-rw-r--r-- 1 claud 197609 60886 Jan 18  2021 gvc.lib
-rw-r--r-- 1 claud 197609  1878 Jan 18  2021 gvplugin_core.lib
-rw-r--r-- 1 claud 197609  1974 Jan 18  2021 gvplugin_dot_layout.lib
-rw-r--r-- 1 claud 197609  1930 Jan 18  2021 gvplugin_gdiplus.lib
-rw-r--r-- 1 claud 197609  2006 Jan 18  2021 gvplugin_neato_layout.lib
-rw-r--r-- 1 claud 197609  1898 Jan 18  2021 gvplugin_pango.lib
-rw-r--r-- 1 claud 197609  5602 Jan 18  2021 pathplan.lib
drwxr-xr-x 1 claud 197609     0 Sep 27 15:38 pkgconfig/
-rw-r--r-- 1 claud 197609  3236 Jan 18  2021 xdot.lib

So, this means my files are in the right location?

I’m not sure what you’re suggesting, I installed make from Chocolatey and gcc is MingGW. What should I try?

If gcc is MinGW it should work. Are you using bash from MinGW as your shell?

Type env and post the result.

I’m using Git Bash. When I type env it’s a really long result:

$ env
USERDOMAIN=DESKTOP-QM67A37
OS=Windows_NT
COMMONPROGRAMFILES=C:\Program Files\Common Files
PROCESSOR_LEVEL=6
PSModulePath=C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
CommonProgramW6432=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
platformcode=KV
MSYSTEM_CARCH=x86_64
DISPLAY=needs-to-be-defined
HOSTNAME=DESKTOP-QM67A37
PUBLIC=C:\Users\Public
CONFIG_SITE=/mingw64/etc/config.site
EXEPATH=C:\Program Files\Git
OnlineServices=Online Services
MSYSTEM_CHOST=x86_64-w64-mingw32
USERNAME=claud
ChocolateyInstall=C:\ProgramData\chocolatey
LOGONSERVER=\\DESKTOP-QM67A37
PROCESSOR_ARCHITECTURE=AMD64
LOCALAPPDATA=C:\Users\claud\AppData\Local
COMPUTERNAME=DESKTOP-QM67A37
!::=::\
SYSTEMDRIVE=C:
USERPROFILE=C:\Users\claud
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW
SYSTEMROOT=C:\WINDOWS
USERDOMAIN_ROAMINGPROFILE=DESKTOP-QM67A37
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 142 Stepping 10, GenuineIntel
MINGW_PACKAGE_PREFIX=mingw-w64-x86_64
OneDriveConsumer=C:\Users\claud\OneDrive
PWD=/c/Users/claud/Documents/UWaterloo/4A/Roseseed/SQLPtoSQL
SSH_ASKPASS=/mingw64/libexec/git-core/git-gui--askpass
HOME=/c/Users/claud
TMP=/tmp
LC_CTYPE=en_US.UTF-8
TERM_PROGRAM=mintty
TERM_PROGRAM_VERSION=3.4.4
MSYSTEM_PREFIX=/mingw64
OneDrive=C:\Users\claud\OneDrive
PROCESSOR_REVISION=8e0a
TMPDIR=/tmp
NUMBER_OF_PROCESSORS=8
ProgramW6432=C:\Program Files
COMSPEC=C:\WINDOWS\system32\cmd.exe
APPDATA=C:\Users\claud\AppData\Roaming
SHELL=/usr/bin/bash
TERM=xterm
WINDIR=C:\WINDOWS
MINGW_CHOST=x86_64-w64-mingw32
ProgramData=C:\ProgramData
SHLVL=1
PLINK_PROTOCOL=ssh
ACLOCAL_PATH=/mingw64/share/aclocal:/usr/share/aclocal
PROGRAMFILES=C:\Program Files
MANPATH=/mingw64/local/man:/mingw64/share/man:/usr/local/man:/usr/share/man:/usr/man:/share/man
ORIGINAL_TEMP=/tmp
ORIGINAL_TMP=/tmp
ALLUSERSPROFILE=C:\ProgramData
TEMP=/tmp
DriverData=C:\Windows\System32\Drivers\DriverData
MSYSTEM=MINGW64
MINGW_PREFIX=/mingw64
asl.log=Destination=file
SESSIONNAME=Console
RegionCode=NA
ProgramFiles(x86)=C:\Program Files (x86)
PATH=/c/Users/claud/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/claud/bin:/c/Program Files/Eclipse Foundation/jdk-11.0.12.7-hotspot/bin:/c/Python39/Scripts:/c/Python39:/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/iCLS:/c/Program Files/Intel/Intel(R) Management Engine Components/iCLS:/c/windows/system32:/c/windows:/c/windows/System32/Wbem:/c/windows/System32/WindowsPowerShell/v1.0:/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/DAL:/c/Program Files/Intel/Intel(R) Management Engine Components/DAL:/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/IPT:/c/Program Files/Intel/Intel(R) Management Engine Components/IPT:/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/c/Program Files/Intel/WiFi/bin:/c/Program Files/Common Files/Intel/WirelessCommon:/c/Program Files/PuTTY:/cmd:/c/Program Files (x86)/nodejs:/c/ProgramData/chocolatey/bin:/c/Gradle/gradle-7.1.1/bin:/c/Kotlin/kotlinc/bin:/c/MinGW/bin:/c/Autoconf/autoconf/bin:/c/FlexBison:/c/Graphviz/lib:/c/Graphviz/include/graphviz:/c/ProgramData/chocolatey/lib/gsudo/bin:/c/Users/claud/AppData/Local/Microsoft/WindowsApps:/c/Users/claud/AppData/Local/Programs/MiKTeX/miktex/bin/x64:/c/Users/claud/AppData/Roaming/npm:/c/Users/claud/AppData/Local/Programs/Microsoft VS Code/bin:/c/Users/claud/AppData/Local/GitHubDesktop/bin:/c/Program Files/Graphviz/bin:/usr/bin/vendor_perl:/usr/bin/core_perl
PS1=\[\033]0;$TITLEPREFIX:$PWD\007\]\n\[\033[32m\]\u@\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$
HOMEDRIVE=C:
ChocolateyLastPathUpdate=132772436600413009
PKG_CONFIG_PATH=/mingw64/lib/pkgconfig:/mingw64/share/pkgconfig
INFOPATH=/usr/local/info:/usr/share/info:/usr/info:/share/info
HOMEPATH=\Users\claud
ORIGINAL_PATH=/mingw64/bin:/usr/bin:/c/Users/claud/bin:/c/Program Files/Eclipse Foundation/jdk-11.0.12.7-hotspot/bin:/c/Python39/Scripts:/c/Python39:/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/iCLS:/c/Program Files/Intel/Intel(R) Management Engine Components/iCLS:/c/windows/system32:/c/windows:/c/windows/System32/Wbem:/c/windows/System32/WindowsPowerShell/v1.0:/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/DAL:/c/Program Files/Intel/Intel(R) Management Engine Components/DAL:/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/IPT:/c/Program Files/Intel/Intel(R) Management Engine Components/IPT:/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/c/Program Files/Intel/WiFi/bin:/c/Program Files/Common Files/Intel/WirelessCommon:/c/Program Files/PuTTY:/cmd:/c/Program Files (x86)/nodejs:/c/ProgramData/chocolatey/bin:/c/Gradle/gradle-7.1.1/bin:/c/Kotlin/kotlinc/bin:/c/MinGW/bin:/c/Autoconf/autoconf/bin:/c/FlexBison:/c/Graphviz/lib:/c/Graphviz/include/graphviz:/c/ProgramData/chocolatey/lib/gsudo/bin:/c/Users/claud/AppData/Local/Microsoft/WindowsApps:/c/Users/claud/AppData/Local/Programs/MiKTeX/miktex/bin/x64:/c/Users/claud/AppData/Roaming/npm:/c/Users/claud/AppData/Local/Programs/Microsoft VS Code/bin:/c/Users/claud/AppData/Local/GitHubDesktop/bin:/c/Program Files/Graphviz/bin
_=/usr/bin/env

I’m not using GitBash when I compile. For that I use MinGW directly. If you’ve installed MinGW (which you indicate by saying gcc is MinGW), try starting a MinGW bash, e.g. by running mingw64.exe or mingw32.exe from PowerShell and try to compile your program that way.

image
Are any of these what you mean? Sorry, I’m not very familiar with MinGW. These are all in the location: C:\MinGW\bin

And if one of them is, how would I then compile my code? What command do you run? And how to I get back to my Makefile? Thanks!

No. Mine is in C:\tools\msys64\mingw64.exe. When run it opens a new terminal window with MinGW bash. In that window, just cd to your directory as usual.

Would you mind letting me know where you downloaded this from? Is there a link I can use? I don’t seem to have it then. Thanks!

Yes, you seem to have a very old gcc as well. I used choco install --yes --no-progress msys2 in PowerShell.

Run as administrator of course. Omit --no-progress if you want to see what’s happening. I just copied the command from our CI where we don’t want any verbosity.

I know. It’s a bit confusing. Try cd /c/Users/claud to your normal home directory.