如何成功运行使用OGDF Dogwood版本的C++文件?
解决OGDF Dogwood版本编译运行ex1.cpp的问题
错误原因分析
-c参数导致生成非可执行文件:g++的-c选项仅完成编译步骤,输出的是目标文件而非可执行程序,因此你生成的ex1.out无法直接运行。- 未定义引用源于缺少库链接:移除
-c后仅编译了源码,但未链接OGDF及其依赖的COIN库,编译器无法找到类和函数的具体实现。 - 找不到库文件是因为路径未指定:构建指南的命令需要明确库文件的搜索路径,否则系统无法定位OGDF和COIN的库文件。
正确编译运行步骤
1. 先完成OGDF的编译
OGDF Dogwood版本必须先编译生成库文件才能使用:
- 进入OGDF根目录(示例路径为
/home/Desktop/OGDF) - 用CMake构建编译:
编译完成后,mkdir build && cd build cmake .. make -j4 # -j4为并行编译,可根据CPU核心数调整数值build/lib目录下会生成libogdf.a(静态库)或libogdf.so(动态库),以及COIN相关库文件。
2. 编译并链接ex1.cpp
执行以下命令完成编译链接,将路径替换为你的实际OGDF路径:
g++ ex1.cpp -I/home/Desktop/OGDF/include -L/home/Desktop/OGDF/build/lib -logdf -lcoin -o ex1.out
参数说明:
-I/home/Desktop/OGDF/include:指定OGDF头文件的搜索路径-L/home/Desktop/OGDF/build/lib:指定OGDF和COIN库文件的搜索路径-logdf:链接OGDF库-lcoin:链接OGDF依赖的COIN库
3. 运行可执行文件
如果使用动态库,需先设置动态库搜索路径,再运行程序:
export LD_LIBRARY_PATH=/home/Desktop/OGDF/build/lib:$LD_LIBRARY_PATH ./ex1.out
运行成功后会生成output-manual.gml和output-manual.svg两个文件。
附ex1.cpp代码
//ex1.cpp #include <ogdf/basic/GraphAttributes.h> #include <ogdf/fileformats/GraphIO.h> using namespace ogdf; int main() { Graph G; GraphAttributes GA(G, GraphAttributes::nodeGraphics | GraphAttributes::edgeGraphics); const int LEN = 11; for (int i = 1; i < LEN; ++i) { node left = G.newNode(); GA.x(left) = -5 * (i + 1); GA.y(left) = -20 * i; GA.width(left) = 10 * (i + 1); GA.height(left) = 15; node bottom = G.newNode(); GA.x(bottom) = 20 * (LEN - i); GA.y(bottom) = 5 * (LEN + 1 - i); GA.width(bottom) = 15; GA.height(bottom) = 10 * (LEN + 1 - i); edge e = G.newEdge(left, bottom); DPolyline &p = GA.bends(e); p.pushBack(DPoint(10, -20 * i)); p.pushBack(DPoint(20 * (LEN - i), -10)); } GraphIO::write(GA, "output-manual.gml", GraphIO::writeGML); GraphIO::write(GA, "output-manual.svg", GraphIO::drawSVG); return 0; }
内容的提问来源于stack exchange,提问作者licheng
相关产品推荐
相关产品推荐

