pow不接受第二个参数作为gcc的变量

Muh*_*edy 0 c math gcc gcc4

pow不接受第二个参数作为gcc的变量

以下代码适用于VC++ 10

// file test.cc
#include "stdafx.h"
#include <stdio.h>
#include <math.h>

int main(void)
{   
    double x = 10;
    int y = 20;
    printf("%f\n", pow(x, y));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是以下代码对gcc不起作用:

// test.c
#include <stdio.h>
#include <math.h>

int main(void)
{   
    double x = 10;
    int y = 20;
    printf("%f\n", pow(x, y)); // error here, says no such function, however when pass the second argument in `pow` for the code runs by gcc, It works fine!
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

geo*_*car 6

你错了.它与第二个参数无关.

在POSIXish系统中pow()是libm,而在win32ish系统中它是标准C库的一部分.意味着代替:

$ gcc program.c
/tmp/ccTw1gCA.o: In function `main':
program.c:(.text+0x30): undefined reference to `pow'
Run Code Online (Sandbox Code Playgroud)

你需要这样做:

$ gcc program.c -lm
Run Code Online (Sandbox Code Playgroud)