未定义的引用`round' - 为什么?我正在使用math.h

时间:2015-12-04 19:27:28

标签: c linux ubuntu gcc

我的源代码有点问题。 gcc跟我说: 未定义引用`round'但我不知道为什么因为我使用的是stdio.h,stdlib.h,math.h ... :-(你可以帮我解决这个问题吗? ?

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <math.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#define VERYBIG 200

int dir_size(const char* dirname)
{
    int size = 0;
    char path[VERYBIG];
    struct stat tmp;
    DIR* cat = opendir(dirname);
    struct dirent* entry;

    while ((entry = readdir(cat)))
    {
        if (!((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)))
        {
            path[0] = 0;
            strcat(path, dirname);
            strcat(path, "/");
            strcat(path, entry->d_name);
            if (lstat(path, &tmp) == -1)
                perror("lstat");

            if (S_ISDIR(tmp.st_mode))
            {
                size += round(tmp.st_blocks / 2);
                size += dir_size(path);
            }
            else
            {
                size += round(tmp.st_blocks / 2);
            }
        }
    }
    return size;
}

void rozmiar(int blocks, int h)
{
    if (h == 0)
        printf("%d\t", blocks);
    else
    {
        double newbl;
        if (blocks < 1024)
            printf("%dK\t", blocks);
        else if (blocks < (1024 * 1024))
        {
            newbl = blocks / 1024;
            printf("%0.1fM\t", round(newbl));
        }
        else
        {
            newbl = blocks / (1024 * 1024);
            printf("%0.1fB\t", round(newbl));
        }
    }
}

void do_du(char dirname[], int s, int h)
{
    DIR* kat;
    struct dirent* wpis;
    int sum = 0, dsize = 0;
    if ((kat = opendir(dirname)) == NULL)
        fprintf(stderr, "du2: cannot open %s\n", dirname);
    struct stat info;
    char path[VERYBIG];

    while ((wpis = readdir(kat)) != NULL)
    {
        if (!((strcmp(wpis->d_name, ".") == 0) || (strcmp(wpis->d_name, "..") == 0)))
        {
            path[0] = 0;
            strcat(path, dirname);
            strcat(path, "/");
            strcat(path, wpis->d_name);

            if (lstat(path, &info) == -1)
                perror("lstat");

            if (S_ISDIR(info.st_mode))
            {
                dsize = dir_size(path) + round(info.st_blocks / 2);
                sum += dsize;
                if (s == 0)
                {
                    rozmiar(dsize, h);
                    printf("%s\n", wpis->d_name);
                }
            }
            else
            {
                sum += round(info.st_blocks / 2);
                if (s == 0)
                {
                    rozmiar(round(info.st_blocks / 2), h);
                    printf("%s\n", wpis->d_name);
                }
            }
        }
    }
    if (stat(dirname, &info) == -1)
        perror("stat");
    sum += round(info.st_blocks / 2);
    rozmiar(sum, h);
    printf("%s\n", dirname);
}

int main(int ac, char* av[])
{
    char optstring[] = "sh";
    int opcja, human = 0, suma = 0;

    while ((opcja = getopt(ac, av, optstring)) != -1)
    {
        switch (opcja)
        {
            case 's':
                suma = 1;
                break;
            case 'h':
                human = 1;
                break;
            case '?':
                printf("Usage: du2 [-s] [-h] [KATALOG]\n\t-s\twyswietla wielkosc "
                       "katalogu\n\t-h\twysiwetla wielkosci plikow w kilo-/mega-/gigabajtach\n");
                exit(1);
        }
    }

    if (human != 0 || suma != 0)
    {
        av++;
        ac--;
    }

    if (ac == 1)
        do_du(".", suma, human);
    else
        do_du(*++av, suma, human);
    return 0;
}

1 个答案:

答案 0 :(得分:7)

它必须是链接器错误。

尝试编译这样的程序

gcc program.c -lm