使用-O0时,g ++停止未定义的引用 [英] g++ stops on undefined reference when using -O0
问题描述
我想调试我写的程序。因此,我想使用-g -O0链接。当我编译(使用autotools Makefile)使用
I want to debug a program I wrote. Therefor I'd like to link it using "-g -O0". When I compile (using autotools Makefile) using
make CXXFLAGS =' - g -O0'
make CXXFLAGS='-g -O0'
我得到一些错误:
libtool:link:g ++ -O2 -g - O0-I / opt / adolc-2.2.1 / include -o oc_poly oc_poly-oc_poly.o
oc_poly-oc_p2p.o -L / opt / ipopt-3.9.3 / lib / coin -L / opt / ipopt -3.9.3 / lib / coin / ThirdParty
-L / usr / lib / i386-linux-gnu / gcc / i686-linux-gnu / 4.5.2 -L / usr / lib / i386-linux-gnu / gcc / i686-
linux-gnu / 4.5.2 /../../... -L / usr / lib / i386-linux-gnu /opt/ipopt-3.9.3/lib/coin/ libipopt.so
-llapack -ldl /opt/ipopt-3.9.3/lib/coin/ThirdParty/libcoinhsl.so /opt/ipopt-3.9.3/lib
/ coin / ThirdParty / libcoinblas。所以/opt/ipopt-3.9.3/lib/coin/ThirdParty/libcoinlapack.so
/opt/ipopt-3.9.3/lib/coin/ThirdParty/libcoinmumps.so -lpthread -lblas -lgfortran -lm
-lgcc_s /opt/ipopt-3.9.3/lib/coin/ThirdParty/libcoinmetis.so -L / opt / adolc-2.2.1 / lib
/opt/adolc-2.2.1/lib/ libadolc.so -Wl,-rpath -Wl,/ opt / ipopt-3.9.3 / lib / coin -Wl,-rpath
-Wl,/ opt / ipopt-3.9.3 / lib / coin / ThirdParty - wl,-rpath -Wl,/ opt / adolc-2.2.1 / lib -Wl,-rpath
-W1,/ opt / ipopt-3.9.3 / lib / coin -Wl,-rpath -Wl, opt / ipopt-3.9.3 / lib / coin / ThirdParty
-Wl,-rpath -Wl,/ opt / adolc-2.2.1 / lib
oc_poly-oc_p2p.o:In functionOCP_P2P :: get_bounds_info(int,double *,double *,int,
OCP_P2P :: INF'
double *,double *)':
/ home / christian / Dokumente / Uni / SA / ist / Berechnungen / Optimale Steuernug / IpOpt
/oc_p2p.cpp:162:未定义引用
libtool: link: g++ -O2 -g -O0 -I/opt/adolc-2.2.1/include -o oc_poly oc_poly-oc_poly.o oc_poly-oc_p2p.o -L/opt/ipopt-3.9.3/lib/coin -L/opt/ipopt-3.9.3/lib/coin/ThirdParty -L/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2 -L/usr/lib/i386-linux-gnu/gcc/i686- linux-gnu/4.5.2/../../.. -L/usr/lib/i386-linux-gnu /opt/ipopt-3.9.3/lib/coin/libipopt.so -llapack -ldl /opt/ipopt-3.9.3/lib/coin/ThirdParty/libcoinhsl.so /opt/ipopt-3.9.3/lib /coin/ThirdParty/libcoinblas.so /opt/ipopt-3.9.3/lib/coin/ThirdParty/libcoinlapack.so /opt/ipopt-3.9.3/lib/coin/ThirdParty/libcoinmumps.so -lpthread -lblas -lgfortran -lm -lgcc_s /opt/ipopt-3.9.3/lib/coin/ThirdParty/libcoinmetis.so -L/opt/adolc-2.2.1/lib /opt/adolc-2.2.1/lib/libadolc.so -Wl,-rpath -Wl,/opt/ipopt-3.9.3/lib/coin -Wl,-rpath -Wl,/opt/ipopt-3.9.3/lib/coin/ThirdParty -Wl,-rpath -Wl,/opt/adolc-2.2.1/lib -Wl,-rpath -Wl,/opt/ipopt-3.9.3/lib/coin -Wl,-rpath -Wl,/opt/ipopt-3.9.3/lib/coin/ThirdParty -Wl,-rpath -Wl,/opt/adolc-2.2.1/lib oc_poly-oc_p2p.o: In function
OCP_P2P::get_bounds_info(int, double*, double*, int, double*, double*)': /home/christian/Dokumente/Uni/SA/ist/Berechnungen/Optimale Steuernug/IpOpt /oc_p2p.cpp:162: undefined reference to
OCP_P2P::INF'
所提到的符号INF是在类定义OCP_P2P中定义的静态类成员。
The mentioned symbol INF is a static class member defined in the class definition OCP_P2P.
如果省略CXXFLAGS或将它们设置为O1, O3或Os,链接工作得很好。我使用g ++ 4.5.2。
If I omit the CXXFLAGS or set them to any optimization value of O1, O2, O3 or Os the linking works pretty well. I am using g++ 4.5.2.
任何人都可以给我一个提示,这里出了什么问题?如何调试我的程序?
Can anybody give me a hint, what's going wrong here? How can I debug my program?
感谢
Christian
Christian
推荐答案
静态类成员变量需要定义;即使它在类定义中仅初始化为一个声明。换句话说,你需要把
A static class member variable needs a definition; even if it is initialised in the class definition that is only a declaration. In other words, you need to put
const double OCP_P2P::INF;
在cpp文件中的某处。假设当优化被打开时,变量的使用被优化。
somewhere in a cpp file. Presumably when optimisation was turned on the use of the variable was optimised out.
官方规则是,如果变量是,则需要一个定义,根据标准的 used 定义,这基本上是指向变量的指针,或者变量绑定到引用。
The official rule is that a definition is needed if the variable is used, according to the standard's definition of used, which is basically if a pointer to the variable is taken, or if the variable is bound to a reference.
这篇关于使用-O0时,g ++停止未定义的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!