在安装 igraph 库后,可以通过以下代码测试是否安装成功。代码创建一个简单的图,并输出其基本信息。
#include <igraph.h>
#include <iostream>
int main() {
igraph_t graph;
igraph_vector_int_t edges;
igraph_vector_int_init(&edges, 0);
igraph_small(&graph, 0, IGRAPH_UNDIRECTED, 0,1, 1,2, 2,3, 3,4, -1);
igraph_write_graph_edgelist(&graph, stdout);
igraph_destroy(&graph);
igraph_vector_int_destroy(&edges);
return 0;
}
编译和运行代码
确保系统已安装 igraph 的开发库。使用以下命令编译代码:
g++ -std=c++11 -I/usr/local/include/igraph -L/usr/local/lib -ligraph test_igraph.cpp -o test_igraph
运行生成的可执行文件:
./test_igraph
预期输出
代码应输出图的边列表,例如:
0 1
1 2
2 3
3 4
检查安装问题
如果编译或运行时出现问题,可能是 igraph 未正确安装。确保:
- 开发库和头文件已安装
- 链接路径和库名称正确
- 编译器支持 C++11 或更高版本
高级测试代码
以下代码测试更多 igraph 功能:
#include <igraph.h>
#include <iostream>
int main() {
igraph_t graph;
igraph_vector_int_t degree;
igraph_vector_int_init(°ree, 0);
igraph_small(&graph, 0, IGRAPH_UNDIRECTED, 0,1, 1,2, 2,3, 3,4, -1);
igraph_degree(&graph, °ree, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS);
std::cout << "Degrees: ";
for (int i=0; i<igraph_vector_int_size(°ree); i++) {
std::cout << VECTOR(degree)[i] << " ";
}
std::cout << std::endl;
igraph_destroy(&graph);
igraph_vector_int_destroy(°ree);
return 0;
}
注意事项
- 确保所有 igraph 对象在使用后销毁
- 检查函数返回值以处理潜在错误
- 对于复杂项目,考虑使用 CMake 或其他构建系统管理编译

585




被折叠的 条评论
为什么被折叠?



