gpt4 book ai didi

c - C 中对 `main' 的 undefined reference

转载 作者:行者123 更新时间:2023-12-02 11:07:13 25 4
gpt4 key购买 nike

您好,我在使用 gcc 编译 C 代码时遇到以下错误

/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

我正在尝试导入 fftw()函数输入 SystemVerilog 。这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <fftw3.h>

void fftw(double FFT_in[],int size)
{

double *IFFT_out;
int i;

fftw_complex *middle;

fftw_plan fft;
fftw_plan ifft;
middle = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*size);
IFFT_out = (double *) malloc(size*sizeof(double));

fft = fftw_plan_dft_r2c_1d(size, FFT_in, middle, FFTW_ESTIMATE); //Setup fftw plan for fft (real 1D data)
ifft = fftw_plan_dft_c2r_1d(size, middle, IFFT_out, FFTW_ESTIMATE); //Setup fftw plan for ifft

fftw_execute(fft);
fftw_execute(ifft);

printf("Input: \tFFT_coefficient[i][0] \tFFT_coefficient[i][1] \tRecovered Output:\n");

for(i=0;i<size;i++)
printf("%f\t%f\t\t\t%f\t\t\t%f\n",FFT_in[i],middle[i][0],middle[i][1],IFFT_out[i]/size);

fftw_destroy_plan(fft);
fftw_destroy_plan(ifft);
fftw_free(middle);
free(IFFT_out);

//return IFFT_out;
}

这是我尝试调用 fftw 的系统 Verilog 代码

module top;
import "DPI-C" function void fftw(real FFT_in[0:11], int size);
real j [0:11];
integer i,size;
real FFT_in [0:11];

initial begin
size = 12;
FFT_in[0] = 0.1;
FFT_in[1] = 0.6;
FFT_in[2] = 0.1;
FFT_in[3] = 0.4;
FFT_in[4] = 0.5;
FFT_in[5] = 0.0;
FFT_in[6] = 0.8;
FFT_in[7] = 0.7;
FFT_in[8] = 0.8;
FFT_in[9] = 0.6;
FFT_in[10] = 0.1;
FFT_in[11] = 0.0;

$display("Entering in SystemVerilog Initial Block\n");
#20
fftw(FFT_in,size);

$display("Printing recovered output from system verilog\n");
//for(i=0;i<size;i++)
//$display("%f\t\n",(j[i])/size);

$display("Exiting from SystemVerilog Initial Block");
#5 $finish;

end

endmodule

这是一个 irun 命令,用于编译 systemverilg 和 C 文件

# Compile the SystemVerilog files
fftw_test.sv
-access +rwc
# Generate a header file called _sv_export.h
-dpiheader _sv_export.h
# Delay compilation of fftw_test.c until after elaboration
#-cpost fftw_test_DPI.c -end
-I/home/fftw/local/include -L/home/ss69/fftw/local/lib fftw_test_DPI.c -lfftw3 -lm
# Redirect output of ncsc_run to a log file called ncsc_run.log
-log_ncsc_run ncsc_run.log

运行此命令时出现以下错误:构建库 run.sold:/home/fftw/local/lib/libfftw3.a(mapflags.o): 创建共享对象时不能使用针对“.rodata”的重定位 R_X86_64_32;使用 -fPIC 重新编译/homefftw/local/lib/libfftw3.a:无法读取符号:错误值Collect2: ld 返回 1 退出状态make: * [/home/ss69/DPI/./INCA_libs/irun.lnx8664.12.20.nc/librun.so] 错误 1ncsc_run: *E,TBBLDF: 无法构建测试库 /home/DPI/./INCA_libs/irun.lnx8664.12.20.nc/librun.so

irun: *E,CCERR: cc 编译时出错(状态1),退出。

当我只是尝试使用 gcc 使用以下命令编译 C 时:
gcc -g -Wall -Werror -I/home/fftw/local/include -L/home/ss69/fftw/local/lib\ fftw_test_DPI.c -lftw3 -lm -o fftw_test_DPI

我收到此错误:

/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../lib64/crt1.o:在函数 _start':
(.text+0x20): undefined reference to
中主要的'collect2:ld返回1退出状态

最佳答案

你到底是如何使用函数void fftw(double FFT_in[],int size)从你的评论来看,听起来你正在编写被称为DLL或作为静态库的一部分的例程.

如果是这种情况,那么添加 main() 根本没有帮助。

如果将您编写的内容用作可调用例程,那么它绝对 100% 正确。

您可能需要做的就是将此例程编译到库中,甚至是静态库。可能没问题。如果是这种情况,请查阅 GCC 文档以了解如何创建静态或动态库。

最后,我自己编写了 Verilog 代码,因此您还可以提供您已阅读并遵循其说明的 Verilog 文档的任何行或引用。我假设在某个时候您正在调用 Verilog 并向其提供它可以/应该使用的库列表。您的库应该包含在该列表中。

根据 jxh 的要求,我添加了他的评论:要将函数导入 SystemVerilog,您需要将函数编译为共享对象。然后,您将 SystemVerilog 指向共享对象。 (我不使用 SystemVerilog,但这是我从其网页收集的信息。)

gcc -shared -fPIC -g -Wall -Werror \
-I/home/ss69/fftw/local/include -L/home/ss69/fftw/local/lib \
fftw_test_DPI.c -lfftw3 -lm -o libfftw_test_DPI.so

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

25 4 0
文章推荐: java - List 和 List 之间的区别