首页 > 解决方案 > pow() 函数不适用于 CLion

问题描述

所以我对这一切都很陌生,但是使用了 YouTube 教程,但由于某种原因,我的 pow 函数不起作用。例如:

printf ("%d", pow(4, 3));

它应该是 %d,因为我得到了双倍的权利吗?我尝试了 %d,但没有奏效。

这是我得到的错误:

error: use of undeclared identifier 'pow'

帮助一个新手,干杯。

标签: cclion

解决方案


我意识到这是一个老问题,但答案实际上并不能解决问题,所以为了后代,这就是答案。

使用 Clion,您需要先添加#include <math.h>到您的代码中,例如

#include <stdio.h>
#include <math.h>


int main() {
    double a, b;
    a = 2.0;
    b = 4.0;

    printf("2.0^4.0 = %f", pow(a, b));

    return 0;
}

第二件事是您需要在 CMakeLists.txt 中添加 target_link_libraries(<target_name> m) 例如

cmake_minimum_required(VERSION 3.20)
project(example C)

set(CMAKE_C_STANDARD 99)

add_executable(example main.c)
target_link_libraries(example m)

推荐阅读