【问题标题】:Coding with c: warning: incompatible implicit declaration of built-in function ‘exp10’用 c 编码:警告:内置函数“exp10”的隐式声明不兼容
【发布时间】:2018-03-17 09:01:39
【问题描述】:

//在这里解决:https://askubuntu.com/questions/962252/coding-with-c-warning-incompatible-implicit-declaration-of-built-in-function

我不明白如何编译。

我没有把我做的所有函数都放在这个库中,因为它们都可以正常工作,而且这是我第一次必须使用 math.h

到目前为止,我已经像这样编译没有问题:

gcc -c -g f.c

gcc -c -g main.c

gcc -o main main.o f.o

我已尝试插入 -lm,但我不知道必须如何以及在何处插入。

//标题

#include<math.h>
#define MAX 5

typedef enum {FALSE, TRUE} bool;

typedef enum {ERROR=-1, OK=1} status;

status parse_int(char s[], int *val);

//功能

#include<stdio.h>
#include<math.h>
#include <stdlib.h>
#include"f.h"


status parse_int(char s[], int *val) {

    int l, val_convertito = 0, val_momentaneo = 0;
    for(l = 0; s[l] != '\0'; l++);
    for(int i = 0; s[i] != '\0'; i++) {
        if(s[i] >= '0' && s[i] <= '9') {
            val_momentaneo = ((int) (s[i]-48)) * ((int)exp10((double)l--)); 
            val_convertito += val_momentaneo;
            *val = val_convertito;
        } else return ERROR;
    }

    return OK;
}

//主要

#include<stdio.h>
#include<math.h>
#include <stdlib.h>
#include"f.h"


int main() {

    int val_con, *val, ls;
    char s_int[ls];

    printf("Inserisci la lunghezza della stringa: ");
    scanf("%d", &ls);

    printf("\n");
    printf("Inserisci l'intero da convertire: \n");
    scanf("%s", s_int);

    val = &val_con;

    status F8 = parse_int(s_int, val);

    switch(F8) {
        case OK:  printf("Valore convertito %d\n", val_con);
                  break;
        case ERROR: printf("E' presente un carattere non numerico.\n");
                    break;
    }

}

【问题讨论】:

  • 尝试将-lm 添加到您的gcc 命令中
  • @EugeneSh。我已经完成了,但我不明白它放在哪里
  • gcc -o main -lm main.o f.o
  • 该错误与链接无关。没有标准的exp10,因此它必须是GCC的扩展,你必须搜索the GCC documentation来找出它在哪个头中声明,如果有的话,并包含那个头文件。
  • @Someprogrammerdude 这确实是一个 GNU 扩展。但是math 库的扩展。所以无论如何它必须与-lm 链接。而且看起来需要#define _GNU_SOURCEman7.org/linux/man-pages/man3/exp10.3.html

标签: c compilation warnings


【解决方案1】:
  1. 此任务确实需要任何 exp10 和 double 值。
  2. 您可以使用 strlen 等标准 C 函数来发现字符串长度(但这里不需要

您的功能可以简化为:

int str_to_int(const char* value)
{
    int res = 0;
    while (*value)
    {
        if (!isdigit(*value)) return -1;
        res *= 10;
        res += *value++ - '0';

    }
    return res;
}

status str_to_int1(const char* value, int *res)
{
    *res = 0;
    while (*value)
    {
        if (!isdigit(*value)) return ERROR;
        *res *= 10;
        *res += *value++ - '0';

    }
    return OK;
}

【讨论】:

  • 谢谢,但这不是我需要的
  • @Zeno Raiser 易于修改您的 typedef - 请参阅第二个
  • 真的非常感谢,但我需要学习如何用 math.h 编译,这就是我的问题的重点
  • 这里不需要数学。 math 用于浮点计算 - 你只做整数。在这项任务中使用数学是错误的做法,如果它是家庭作业 - 不应该被接受。
  • 我明白你在说什么,但我想学习如何用math.h编译ti,是否需要它不是事实
猜你喜欢
  • 2010-11-01
  • 2013-12-01
  • 2015-03-18
  • 1970-01-01
  • 2013-02-15
  • 2012-10-29
  • 1970-01-01
  • 2013-12-28
  • 2011-12-15
相关资源
最近更新 更多