c - 对 'nCr' 的 undefined reference

标签 c linux reference linker undefined

我正在尝试编译我的程序以在 Linux 上运行,但由于这个错误它无法运行。

我用 gcc -ansi -pedantic -Wall -m64 -o formula formula.c nCr.s 编译,我得到的错误是:

/tmp/ccnNz7Jr.o: In function `main':
formula.c:(.text+0xc1): undefined reference to `nCr'
collect2: ld returned 1 exit status
make: *** [build] Error 1

这是文件 formula.c 中的代码

#include <stdio.h>
#include <stdlib.h>
#include "nCr.h"
#include <time.h>
#include <sys/time.h>

int main(int argc, const char * argv[])
{
int x = 0;
int y;
int z;

struct timeval start, end;

if (argv[1][0] == '-' && argv[1][1] == 'h') {
    printf("Usage: formula <positive integer>");
} else {
    y = atoi(argv[1]);

    gettimeofday(&start, NULL);

    printf("(1 + x)^%i = 1+", y);

    if (y == 0)
        printf("0");


    if (y > 12) {
        printf("%s\n","Please enter a number 12 or below. Anything higher results in overflow, the answer you want is not the answer you will get.");
    }

    for (; x <= y; x++) {

        z = nCr(y, x);

        if (z == -1) {
            printf("Multiplication overflow. \n");
            return 1;
        } else {
            if (x != 0)
                printf("%i x^%i ",z , x);

            if (x != y && x != 0)
                printf("+ ");
        }
    }

    gettimeofday(&end, NULL);

}

printf("\n%ld microseconds\n", ((end.tv_sec * 1000000 + end.tv_usec)
      - (start.tv_sec * 1000000 + start.tv_usec)));

return 0;
}

ncr.h

#ifndef _NCR_H_
#define _NCR_H_

extern int Factorial(int n);

extern int nCr(int n, int r);

#endif /* _NCR_H_ */

还有一个叫nCr.s的汇编文件

    .globl _factorial
    _factorial:
    Leh_func_begin2:
    pushq   %rbp
    Ltmp3:
movq    %rsp, %rbp
Ltmp4:
movl    %edi, -4(%rbp)
movl    $1, -16(%rbp)
movl    $1, -20(%rbp)
jmp LBB2_2
LBB2_1:
movl    -16(%rbp), %eax
movl    -20(%rbp), %ecx
imull   %ecx, %eax
movl    %eax, -16(%rbp)
movl    -20(%rbp), %eax
addl    $1, %eax
movl    %eax, -20(%rbp)
LBB2_2:
movl    -20(%rbp), %eax
movl    -4(%rbp), %ecx
cmpl    %ecx, %eax
jle LBB2_1
movl    -16(%rbp), %eax
movl    %eax, -12(%rbp)
movl    -12(%rbp), %eax
movl    %eax, -8(%rbp)
movl    -8(%rbp), %eax
popq    %rbp
ret

.globl _nCr
_nCr:
Leh_func_begin1:
pushq   %rbp
Ltmp0:
movq    %rsp, %rbp
Ltmp1:
subq    $32, %rsp
Ltmp2:
movl    %edi, -4(%rbp)
movl    %esi, -8(%rbp)
movl    -4(%rbp), %eax
xorb    %cl, %cl
movl    %eax, %edi
movb    %cl, %al
callq   _factorial
movl    %eax, %ecx
movl    %ecx, -20(%rbp)
movl    -4(%rbp), %ecx
movl    -8(%rbp), %edx
subl    %edx, %ecx
movl    %ecx, -24(%rbp)
movl    -8(%rbp), %ecx
xorb    %dl, %dl
movl    %ecx, %edi
movb    %dl, %al
callq   _factorial
movl    %eax, %ecx
movl    -24(%rbp), %edx
xorb    %sil, %sil
movl    %edx, %edi
movb    %sil, %al
movl    %ecx, -32(%rbp)
callq   _factorial
movl    %eax, %ecx
movl    -32(%rbp), %esi
imull   %ecx, %esi
movl    %esi, -28(%rbp)
movl    -20(%rbp), %ecx
movl    -28(%rbp), %esi
movl    %ecx, %eax
cltd
idivl   %esi
movl    %eax, %ecx
movl    %ecx, -16(%rbp)
movl    %ecx, -12(%rbp)
movl    -12(%rbp), %eax
addq    $32, %rsp
popq    %rbp
ret

非常感谢任何帮助,似乎无法弄清楚为什么它没有编译。它在 mac 上编译得很好,但我需要让它在 linux 上运行。

最佳答案

GCC 无法找到 extern int Factorial(int n);extern int nCr(int n, int r); 定义,因为它们被称为 _factorial:_nCr: 在你的 asm 文件中。更改这些标签以匹配声明,您的代码将编译。

我注意到您在 main 函数中只有一次 nCr 调用,所以我认为最好更改 .h 中的 decls。所以你的 nCr.h 将是:

#ifndef _NCR_H_
#define _NCR_H_

extern int _factorial(int n);

extern int _nCr(int n, int r);

#endif /* _NCR_H_ */

而且你必须在 main 调用 _nCr(...)

关于c - 对 'nCr' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37076426/

相关文章:

C -- Double 类型数据 : Number slightly larger than allowed is able to slip through the conditions

linux - 将只读驱动器安装为写入驱动器

linux - Linux 中的蓝牙配置文件信息

linux - Linux 中的 Netbeans 和 Xdebug

c - 使用 C 编程的连续整数

c - GCC C 认为我将函数声明为静态的?

c++ - C++中的变量和引用之间有什么区别?

c++ - 通过对基类的引用调用虚函数

C/symbol 中的 const 数组在使用 nm 编译的 *.o 文件中不可见

c++ - 方法声明中 & 符号的含义