Hi I am getting below error while compiling a c code using gcc
Hi,我在使用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
I am trying to import the fftw()
function into SystemVerilog. Here is my code
我正在尝试将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;
}
Here is a system Verilog code from where I am trying to call fftw
这是一个系统的Verilog代码,我在这里尝试调用fftw。
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
Here is an irun command to compile both systemverilg and C files
这里有一个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
while running this command give below error: building library run.so ld: /home/fftw/local/lib/libfftw3.a(mapflags.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC /homefftw/local/lib/libfftw3.a: could not read symbols: Bad value collect2: ld returned 1 exit status make: * [/home/ss69/DPI/./INCA_libs/irun.lnx8664.12.20.nc/librun.so] Error 1 ncsc_run: *E,TBBLDF: Failed to build test library /home/DPI/./INCA_libs/irun.lnx8664.12.20.nc/librun.so
在运行此命令时给出如下错误:构建库运行。所以ld: /home/fftw/local/lib/libfftw3.a(mapflags.o):重新定位R_X86_64_32。在创建共享对象时不能使用rodata;编译与当地- fpic homefftw / / lib / libfftw3。a:不能读取符号:Bad value collect2: ld返回1退出状态:* [/home/ss69/DPI/./INCA_libs/irun.lnx8664.12.20.nc/librun。因此,错误1 ncsc_run: *E,TBBLDF:未能构建测试库/home/ dpi/ ./ inca_libs/irun.lnx8664.12.20 .nc/ librunso。
irun: *E,CCERR: Error during cc compilation (status 1), exiting.
irun: *E,CCERR:在cc编译时出错(状态1),退出。
When I simply try to compile C using gcc with below command:
gcc -g -Wall -Werror -I/home/fftw/local/include -L/home/ss69/fftw/local/lib \ fftw_test_DPI.c -lfftw3 -lm -o fftw_test_DPI
当我简单地尝试编译C时,使用以下命令:gcc -g -Wall -Werror -I/home/fftw/local/包括-L/home/ss69/fftw/local/lib \ fftw_test_DPI。c -lfftw3 -lm -o fftw_test_DPI。
I get this error:
我得到这个错误:
/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
/usr/lib/gcc/x86_64-redhat-linux 4.4.6 / . . / . . / . . / . . / lib64 / crt1。o:在函数_start: (.text+0x20):未定义的引用tomain' collect2: ld返回1退出状态。
5 个解决方案
#1
3
Exactly how are you using the function void fftw(double FFT_in[],int size)
from your comments it sounds like you are coding routine that is called as DLL or as part of a static library.
确切地说,您如何使用函数void fftw(double FFT_in[],int size)从您的评论中,听起来好像您是编码例程,被称为DLL或作为静态库的一部分。
If this is the case then adding main()
isn't going to help, at all.
如果是这样,那么添加main()根本就不会有帮助。
What you have written is ABSOLUTELY 100% OK, if it is to be used as a callable routine.
你所写的绝对是100% OK,如果它被用作一个可调用的例程。
What you might need to do is compile this routine into a library, even a static lib. is probably ok. If this is the case then consult your GCC documentation on how to create a static or dynamic lib.
您可能需要做的是将这个例程编译到一个库中,即使是静态的lib也可以。如果是这样,请参考GCC文档,了解如何创建静态或动态库。
Finally, I have written Verilog code myself, so you can also provide any lines or references to Verilog documentation that you have read and whose instructions you are following. I assume that at some point you are invoking Verilog and supplying it with a list of libraries it can/should use. Your lib should be included in that list.
最后,我自己编写了Verilog代码,因此您还可以提供您所阅读的Verilog文档的任何行或引用,以及您所遵循的指示。我假设在某个时刻,您正在调用Verilog并向它提供它可以/应该使用的库列表。您的lib应该包含在该列表中。
Am including comments from jxh per his request: To import the function into SystemVerilog, you need to compile your function into a shared object. Then, you would point SystemVerilog at the shared object. (I don't use SystemVerilog, but that is what I gather from its web page.)
包括jxh对其请求的注释:将函数导入SystemVerilog中,您需要将函数编译成一个共享对象。然后,在共享对象上点SystemVerilog。(我不使用SystemVerilog,但这是我从它的web页面收集的内容。)
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
#2
3
- Your are missing
#include "svdpi.h"
in the fftwc.c file (or maybe you are not showing it because it is in fftwc.h). This include is needed for DPI. - 你丢失的#包括“svdpi”。fftwc h”。c文件(或者你没有显示它,因为它在fftwc.h)。这包括了DPI的需要。
- You are compiling a DPI library to be used with a SystemVerilog simulator. Therefore, you do not need a
main()
method. - 您正在编译一个与SystemVerilog模拟器一起使用的DPI库。因此,您不需要main()方法。
- I prefer to always compile all DPI methods outside of the SystemVerilog compiler. The include the DPI library to the simulation phase. My flow looks something like the following:
- 我更喜欢在SystemVerilog编译器之外编译所有的DPI方法。将DPI库包含到模拟阶段。我的流程如下:
${SVTOOL} -compile -f svfiles.f -dpi_header gen_dpi_header.h
gcc -fPIC -pipe -O2 -c -g \
-I${SVTOOL_PATH}/include -Imy_dpi_dir -I. \
-o fftw_test_DPI.o \
fftw_test_DPI.c
gcc -shared -o libdpi.so \
fftw_test_DPI.o [other object files]
# then call ${SVTOOL} again for simulation with libdpi.so
If you cannot get past the first gcc stage then your issue is clearly on the C side.
如果你无法通过第一个gcc阶段,那么你的问题显然是在C方面。
I do not have access to the fftw3 library at the moment. I'm wondering your void fftw(double FFT_in[],int size)
might be clobbering a library function. Try renaming it void dpi_fftw(double FFT_in[],int size)
我现在没有办法进入fftw3图书馆。我想知道您的void fftw(double FFT_in[],int size)可能会阻塞一个库函数。尝试重新命名为void dpi_fftw(双FFT_in[],int size)
#3
1
Have found the following tutorial on Dynamic Programming Interface (DPI) :
在动态编程接口(DPI)中找到了以下教程:
http://www.doulos.com/knowhow/sysverilog/tutorial/dpi/
Specifically, scroll down to the "Including Foreign Language Code".
具体来说,向下滚动到“包括外语代码”。
It should help with background information about how to construct a C modules for SystemVerilog.
它应该有助于提供关于如何构建SystemVerilog的C模块的背景信息。
Also, the tutorial has the following import
statement:
另外,本教程有以下导入语句:
import "DPI" function void slave_write(input int address, input int data);
This SystemVerilog statement obviously has input
defs on the parameters, is this required? Your import
does NOT identify input vs. output??
这个SystemVerilog语句显然在参数上输入了defs,这是必需的吗?您的导入没有识别输入和输出??
#4
0
You have no main function. Every binary must define main. If it doesn't, you don't have a null region of memory that _start defines in the binary, which means your program can't start!
你没有主函数。每个二进制文件都必须定义main。如果它没有,那么您就没有一个在二进制中定义的零内存区域,这意味着您的程序无法启动!
Add a function:
添加一个函数:
int main(){
fftw(doubleArgumentsArray, intArgument); //Or whatever function calls this function
return 1; //Needed for C89, C99 will automatically return 1
}
#5
0
I believe this is an issue with some gcc linkers. I added the following linker flag:
我认为这是一些gcc连接器的问题。我添加了以下链接标志:
irun ... -Wld,-B/usr/lib/x86_64-linux-gnu
And it fixed the issue.
它解决了这个问题。
#1
3
Exactly how are you using the function void fftw(double FFT_in[],int size)
from your comments it sounds like you are coding routine that is called as DLL or as part of a static library.
确切地说,您如何使用函数void fftw(double FFT_in[],int size)从您的评论中,听起来好像您是编码例程,被称为DLL或作为静态库的一部分。
If this is the case then adding main()
isn't going to help, at all.
如果是这样,那么添加main()根本就不会有帮助。
What you have written is ABSOLUTELY 100% OK, if it is to be used as a callable routine.
你所写的绝对是100% OK,如果它被用作一个可调用的例程。
What you might need to do is compile this routine into a library, even a static lib. is probably ok. If this is the case then consult your GCC documentation on how to create a static or dynamic lib.
您可能需要做的是将这个例程编译到一个库中,即使是静态的lib也可以。如果是这样,请参考GCC文档,了解如何创建静态或动态库。
Finally, I have written Verilog code myself, so you can also provide any lines or references to Verilog documentation that you have read and whose instructions you are following. I assume that at some point you are invoking Verilog and supplying it with a list of libraries it can/should use. Your lib should be included in that list.
最后,我自己编写了Verilog代码,因此您还可以提供您所阅读的Verilog文档的任何行或引用,以及您所遵循的指示。我假设在某个时刻,您正在调用Verilog并向它提供它可以/应该使用的库列表。您的lib应该包含在该列表中。
Am including comments from jxh per his request: To import the function into SystemVerilog, you need to compile your function into a shared object. Then, you would point SystemVerilog at the shared object. (I don't use SystemVerilog, but that is what I gather from its web page.)
包括jxh对其请求的注释:将函数导入SystemVerilog中,您需要将函数编译成一个共享对象。然后,在共享对象上点SystemVerilog。(我不使用SystemVerilog,但这是我从它的web页面收集的内容。)
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
#2
3
- Your are missing
#include "svdpi.h"
in the fftwc.c file (or maybe you are not showing it because it is in fftwc.h). This include is needed for DPI. - 你丢失的#包括“svdpi”。fftwc h”。c文件(或者你没有显示它,因为它在fftwc.h)。这包括了DPI的需要。
- You are compiling a DPI library to be used with a SystemVerilog simulator. Therefore, you do not need a
main()
method. - 您正在编译一个与SystemVerilog模拟器一起使用的DPI库。因此,您不需要main()方法。
- I prefer to always compile all DPI methods outside of the SystemVerilog compiler. The include the DPI library to the simulation phase. My flow looks something like the following:
- 我更喜欢在SystemVerilog编译器之外编译所有的DPI方法。将DPI库包含到模拟阶段。我的流程如下:
${SVTOOL} -compile -f svfiles.f -dpi_header gen_dpi_header.h
gcc -fPIC -pipe -O2 -c -g \
-I${SVTOOL_PATH}/include -Imy_dpi_dir -I. \
-o fftw_test_DPI.o \
fftw_test_DPI.c
gcc -shared -o libdpi.so \
fftw_test_DPI.o [other object files]
# then call ${SVTOOL} again for simulation with libdpi.so
If you cannot get past the first gcc stage then your issue is clearly on the C side.
如果你无法通过第一个gcc阶段,那么你的问题显然是在C方面。
I do not have access to the fftw3 library at the moment. I'm wondering your void fftw(double FFT_in[],int size)
might be clobbering a library function. Try renaming it void dpi_fftw(double FFT_in[],int size)
我现在没有办法进入fftw3图书馆。我想知道您的void fftw(double FFT_in[],int size)可能会阻塞一个库函数。尝试重新命名为void dpi_fftw(双FFT_in[],int size)
#3
1
Have found the following tutorial on Dynamic Programming Interface (DPI) :
在动态编程接口(DPI)中找到了以下教程:
http://www.doulos.com/knowhow/sysverilog/tutorial/dpi/
Specifically, scroll down to the "Including Foreign Language Code".
具体来说,向下滚动到“包括外语代码”。
It should help with background information about how to construct a C modules for SystemVerilog.
它应该有助于提供关于如何构建SystemVerilog的C模块的背景信息。
Also, the tutorial has the following import
statement:
另外,本教程有以下导入语句:
import "DPI" function void slave_write(input int address, input int data);
This SystemVerilog statement obviously has input
defs on the parameters, is this required? Your import
does NOT identify input vs. output??
这个SystemVerilog语句显然在参数上输入了defs,这是必需的吗?您的导入没有识别输入和输出??
#4
0
You have no main function. Every binary must define main. If it doesn't, you don't have a null region of memory that _start defines in the binary, which means your program can't start!
你没有主函数。每个二进制文件都必须定义main。如果它没有,那么您就没有一个在二进制中定义的零内存区域,这意味着您的程序无法启动!
Add a function:
添加一个函数:
int main(){
fftw(doubleArgumentsArray, intArgument); //Or whatever function calls this function
return 1; //Needed for C89, C99 will automatically return 1
}
#5
0
I believe this is an issue with some gcc linkers. I added the following linker flag:
我认为这是一些gcc连接器的问题。我添加了以下链接标志:
irun ... -Wld,-B/usr/lib/x86_64-linux-gnu
And it fixed the issue.
它解决了这个问题。