Undefined Reference Error C++

I am able to compile my program on CLion using both MinGW and Cygwin with gcc and g++ for C and C++ respectively. I can also create objects as follows.

extern "C" {
#include <igraph.h>
}

#include <iostream>
#include <fstream>
#include "Constants.h"
#include "NursingHome.h"


int main() {

    // Create a graph object

    igraph_t graph;
    igraph_vector_t edges;
    igraph_sir_t sim;

//    igraph_empty(&graph, 0, 0);

//    igraph_full(&graph, Constants::numOfFacilities, IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS);
//    igraph_add_vertices(&graph, Constants::numOfFacilities, new NursingHome);

    std::cout << "Success" << std::endl;
    return 0;
}

However, when I run any of the commented out lines, I get an undefined reference error. For example:

CMakeFiles\NetworkModel.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Kyle/Downloads/Epi/Code/NetworkModel/main.cpp:38: undefined reference to `igraph_empty'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [NetworkModel.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/NetworkModel.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/NetworkModel.dir/rule] Error 2
mingw32-make.exe: *** [NetworkModel] Error 2
CMakeFiles\NetworkModel.dir\build.make:136: recipe for target 'NetworkModel.exe' failed
CMakeFiles\Makefile2:93: recipe for target 'CMakeFiles/NetworkModel.dir/all' failed
CMakeFiles\Makefile2:100: recipe for target 'CMakeFiles/NetworkModel.dir/rule' failed
Makefile:136: recipe for target 'NetworkModel' failed

I tried to follow the steps from this post and I changed igraph to a shared library, but I still wasn’t able to fix this error.

Note: in case this is relevant, I run CLion on Windows 10 but installed igraph via Ubuntu running on WSL2.

It looks like you are not linking to the igraph library. Please check the tutorial, which explains how to do it: igraph Reference Manual I believe CLion uses CMake, so the CMakeLists.txt file from the tutorial should work.

While I am not very familiar with Cygwin, I think you should not be mixing the two compilers (from MinGW and Cygwin). Use the same compiler both for igraph and your own program.

extern "C" is not necessary here.

Hi,

I’ve read that reference manual page but I haven’t been able to get the example CMakeLists.txt file working. Here is my current CMakeLists.txt file

cmake_minimum_required(VERSION 3.19)
set(CMAKE_CXX_STANDARD 14)
project(NetworkModel)

add_executable(NetworkModel main.cpp NursingHome.cpp NursingHome.h MemoryCareFacility.cpp MemoryCareFacility.h Constants.h)

include_directories(igraph-0.9.4 igraph-0.9.4/include igraph-0.9.4/build/include)

Thank you!

Can you be more specific please? If you got an error, show what it was.

This is not correct because it does not link to igraph. It only specifies the include paths for the build directory, but this is not the usual way to use igraph. You should install it (make install or cmake --install ., see igraph Reference Manual). If it is not installed in a standard location (on Windows there isn’t really any standard location), then you need to tell CMake where it is using CMAKE_PREFIX_PATH when using it. Then the CMakeLists.txt from the documentation will work.

Hi,

I rebuilt and installed igraph using the CMAKE_PREFIX_PATH as part of the cmake … command, just like in the documentation. Using the CMakeLists.txt file from the documentation, I get the following error.

CMake Error at CMakeLists.txt:16 (find_package):
  By not providing "Findigraph.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "igraph", but
  CMake did not find one.

  Could not find a package configuration file provided by "igraph" with any
  of the following names:

    igraphConfig.cmake
    igraph-config.cmake

  Add the installation prefix of "igraph" to CMAKE_PREFIX_PATH or set
  "igraph_DIR" to a directory containing one of the above files.  If "igraph"
  provides a separate development package or SDK, be sure it has been
  installed.


-- Configuring incomplete, errors occurred!

Thank you so much for your help!!

For installation, you need to use CMAKE_INSTALL_PREFIX to set the install location.

It is when using igraph that you need to set CMAKE_PREFIX_PATH to the same path where igraph was installed (same as you gave in CMAKE_INSTALL_PREFIX).

Hi,

Thank you again for your help. I believe that CLion is now recognizing igraph, but I’m having trouble with configuration errors:

Could not find a configuration file for package "igraph" that is compatible
  with requested version "".

  The following configuration files were considered but not accepted:

    C:/Users/Kyle/Downloads/Epi/Code/NetworkModel/igraph-0.9.4/lib/cmake/igraph/igraph-config.cmake, version: 0.9.4 (64bit)


-- Configuring incomplete, errors occurred!

Thank you!

The problem is that I do not know what you are doing exactly, so I cannot repeat the steps you are using. For me, it works fine. In order to determine why it does not work for you, we need to find out what we are doing differently, and for that I would need to know the precise steps you were following.

CLion makes that difficult as it has so many GUI settings. I suggest doing it from the command line first, then figuring out how to replicate the command line steps in CLion.

Here are the steps I am following. This is on macOS, but it should work the same on Windows, except that you will need to choose a different installation location, and specify the full path to it. I used $HOME/iginstall. You need to replace that with your chosen installation directory.

In the igraph source directory:

mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/iginstall
cmake --build . --config Release
cmake --install .

Now create a directory for your own project. Put the tutorial program into a file named igraph_test.c and use the CMakeLists.txt file verbatim from the tutorial.

Now use similar steps:

mkdir build
cd build
cmake .. -DCMAKE_PREFIX_PATH=$HOME/iginstall
cmake --build . --config Release

Now the example program is compiled and is ready to run.

The --config Release part is only necessary if you are using the Visual Studio CMake generator.

Thank you for providing this info. Maybe there’s a problem with the structure of my project? Everything exists in a directory called NetworkModel, including my .cpp and .h files and the igraph-0.9.4 directory. As well as the igraph_test.c file from your example that I followed. I’m not sure what else could be causing the problem.

I was able to fix the configuration error but now I’m back to my original issue with undefined references to functions. I made sure that the CMAKE_PREFIX_PATH variables are correct so I believe igraph is being recognized. So I’m not sure why the function calls give an undefined reference.

Thank you