Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当一个参数是函数的返回值时,调用pow()时发生链接器错误_C++_Reference_G++_Undefined_Pow - Fatal编程技术网

C++ 当一个参数是函数的返回值时,调用pow()时发生链接器错误

C++ 当一个参数是函数的返回值时,调用pow()时发生链接器错误,c++,reference,g++,undefined,pow,C++,Reference,G++,Undefined,Pow,考虑以下代码: #include "../datatypes/stdam.h" using namespace std; int main(){ int val = pow(pow(3,2),2); cout << val; return 0; } 我哪里做错了?导致上述错误的原因是什么?将标题更改为: #ifndef _MATH_H_ #define _MATH_H_ extern "C" double pow( dou

考虑以下代码:

    #include "../datatypes/stdam.h"
    using namespace std;

    int main(){
    int val = pow(pow(3,2),2);
    cout << val;
    return 0;
    }
我哪里做错了?导致上述错误的原因是什么?

将标题更改为:

#ifndef _MATH_H_
#define _MATH_H_

extern "C"
double pow( double x, double y );

#endif
你真的应该包括math.h或cmath


无论如何,请参见了解
extern“C”

的重要性在第一种情况下,编译器能够在编译时计算
pow()
;如果没有实际调用,则不需要数学库。在第二种情况下,编译器生成一个实际调用。默认情况下不会链接数学库(至少对于您正在使用的编译器是如此)。g++-lm-o test_1 test_1.cpp。我已经使用上面的命令链接了数学库。它仍然显示相同的错误。您能尝试一下
g++-o test\u 1 test\u 1.cpp-lm
?一些链接器认为,在它在
test_1.cpp
中真正看到未解析的符号之前,它实际上不需要链接
libm
。我也尝试过
g++-o test_1 test_1.cpp-lm
。还是一样的错误。为了向您提供有关我的stdam.h文件(旨在限制某些功能)的更多信息,…下面是如何在其中定义pow()。
\ifndef\u MATH\u h\35; define\u MATH\u h\u double pow(double,double)#endif
 /tmp/ccxC17Ss.o: In function `main':
 test_1.cpp:(.text+0x2f): undefined reference to `pow(double, double)'
 collect2: ld returned 1 exit status
#ifndef _MATH_H_
#define _MATH_H_

extern "C"
double pow( double x, double y );

#endif