C API for Unix and Windows to create graphs and compute the shortest paths with Dijkstra
-
Install Visual Studio (e.g. Community) choosing the following
single components:MSVC v142+ - C++ Build toolsC++/CLI for Build Tools v142+Windows 11 SDKCMake C++ Tools for Windows
-
Add the following environmental variable as User:
VS_DIR: Path to Visual Studio with all folders (e.g.Microsoft Visual Studio\2022\Community)
-
Start a
Command Prompt(no PowerShell) and type the following to setup the env:"%VS_DIR%\VC\Auxiliary\Build\vcvarsall.bat" x64
No prerequisites
-
Download the repository
-
Run the following in a terminal (or Command Prompt) to install the library:
cd graphoc cmake -B build -S . -L -DCMAKE_INSTALL_PREFIX=<install_folder> cmake --build build --target install --config ReleaseNote: Generate shared libraries (
.soor.lib+.dll) instead as follows:cmake -B build -S . -L -DCMAKE_INSTALL_PREFIX=<install_folder> -DSHARED=ON -
When using the library in your project make sure to:
- Include
<install_folder>/include - Link the library
<install_folder>/lib/graphoc.aor<install_folder>/lib/graphoc.lib
- Include
In your CMakeLists.txt file
...
find_package(graphoc PATHS "<install_folder>" REQUIRED)
include_directories(${graphoc_INCLUDE_DIR})
add_executable(${PROJECT_NAME} yourmain.cpp)
target_link_libraries(${PROJECT_NAME} "${graphoc_LIBRARIES}")
install(FILES ${graphoc_RUNTIME} DESTINATION bin)
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION lib)
...
#include "graph.h"
#include "spaths.h"
...
int main(int argc, char* argv[]) {
graph_t* graph;
double *dist = NULL;
unsigned int * predecessors=NULL;
char * labels[] = {
"node1",
"node2",
"node3",
...,
NULL,
}
char* edges[] = {
"node1:node2:20.0",
"node2:node3:40.0",
...,
NULL,
};
if ( ( graph = create_graph(<number_of_nodes>, labels) ) == NULL ) {
exit(EXIT_FAILURE);
}
for(char** e=edges; *e!=NULL; e++){
if ( add_edge(graph,*e) == -1 ) {
perror("Adding edge error");
exit(EXIT_FAILURE);
}
}
if ( ( dist = dijkstra(graph, 0, &predecessors) ) == NULL ) {
perror("Dijkstra error");
exit(EXIT_FAILURE);
}
/* Use vector of distances 'dist' and 'predecessors' from 'node1' */
free_graph(&graph);
...
}
cmake -B build -S . -L -DCMAKE_INSTALL_PREFIX=<install_folder>
cmake --build .\build --target install --config Debug
cd build
ctest --build-config Debug --build-target install