C编译错误:未声明(在此函数中首次使用)

时间:2022-11-27 23:40:43

On the PI, I needed the i2c.so library using this git: https://github.com/silentbobbert/pi_sensors. When running makefile from this git to get the i2c.so, i received this error:

在PI上,我需要i2c。所以使用这个git的库:https://github.com/silentbobbert/pi_传感器。从这个git中运行makefile来获得i2c。所以,我收到了这个错误:

C编译错误:未声明(在此函数中首次使用)C编译错误:未声明(在此函数中首次使用)

Here are the .c and the .h files: https://github.com/silentbobbert/pi_sensors/tree/master/Info/LinuxInterface

这里是.c和。h文件:https://github.com/silentbobbert/pi_sensors/tree/master/Info/LinuxInterface。

For reference, here is the contents of makefile:

这里是makefile的内容:

SHELL = /bin/sh
CC    = gcc
FLAGS        = -c -Wall -Werror -fpic
DEBUGFLAGS   = -O0 -D _DEBUG
RELEASEFLAGS = -O2 -D NDEBUG

TARGET  = i2c.so
SOURCES = $(shell echo *.c)
HEADERS = $(shell echo *.h)
OBJECTS = $(SOURCES:.c=.o)

PREFIX = $(DESTDIR)/usr/local
BINDIR = $(PREFIX)/bin

all:
    $(CC) $(FLAGS) $(RELEASEFLAGS) $(SOURCES)
    $(CC) -shared -o $(TARGET) $(OBJECTS)

1 个解决方案

#1


1  

as others have said, this is C code not C#.

正如其他人所说,这是C代码而不是c#。

Anyhow, you have two errors (and they do not relate directly) to the makefile but rather your compilation environment and the code itself.

无论如何,您有两个错误(它们并没有直接关联)到makefile,而是您的编译环境和代码本身。

OK, so how to approach something like this. First notice the first line in your screen-capture, that is the command that is being executed that is generating the error messages, I'll reproduce it here for you;

好,那么如何处理这样的问题。首先注意屏幕截图中的第一行,这是正在执行的命令生成错误消息,我将在这里为您复制它;

gcc -c -Wall -Werror -fpic -O2-D NDEBUG i2c_get.c i2c_set.c i2cbusses.c i2cset.c util.c

With this command we are compiling (note the -c flag) a bunch of source files into a single object file, the presence of the `c' flag implies that no linking is performed here. This is relevant so we know where along the tool-chain we are, and the type of errors we can expect (typically either syntax errors or missing header files).

通过这个命令,我们正在编译(注意-c标志)一堆源文件到一个单一的对象文件中,“c”标志的存在意味着这里不执行链接。这是相关的,因此我们知道我们在工具链上的位置,以及我们可以预期的错误类型(通常是语法错误或缺少头文件)。

The first error;

第一个错误;

i2cset.c: In function 'check_funcs'
i2cset.c:56:2 error: implicit declaration of function 'iotcl' [-Werror=implicit-function-declaration]

is kinda saying, "hey, I can see that ioctl is a function, but you haven't told me anything about it so I'm going to assume that its signature is int ioctl() — a function with an indeterminate (but fixed, not variadic) argument list that returns an int". Given that you are compiling on a Linux-based system, adding #include <sys/ioctl.h> to the top of the file should fix this error.

就是说,我可以看到ioctl是一个函数,但是你还没有告诉我它的任何信息,所以我要假设它的签名是int ioctl()——一个带有不确定(但是是固定的,不是可变的)参数列表的函数,它返回一个整数。考虑到您正在基于linux的系统进行编译,添加#include 应该修正这个错误。

The second error;

第二个错误;

i2cset.c:63:7: error: 'I2C_SMBUS_BYTE' undeclared (first use in this function)

is related to the first error; and it is kinda saying 'hey, you haven't told me anything about I2C_SMBUS_BYTE'. Again, the most common reason for seeing this error is a missing header file. Looking at the source files you've provided a link to, it seems that I2C_SMBUS_BYTE is defined in the header file i2c-dev.h, which however appears to be included in i2cset.c by: #include <linux/i2c-dev.h>.

与第一个错误有关;它会说,嘿,你没有告诉我I2C_SMBUS_BYTE的任何信息。同样,看到这个错误最常见的原因是缺少头文件。查看您提供的链接的源文件,看起来I2C_SMBUS_BYTE是在头文件i2c-dev中定义的。h,但是似乎包含在i2cset中。c:# include < linux / i2c-dev.h >。

At this point I'd insure that your compiler (gcc) can find the header file. From the error messages you are getting, I'm guessing that it is not, but you should be seeing an error message from the compiler about not being able to find the file. Also, if the file is on your system check to see if has the appropriate contents as compared to the git site.

在这一点上,我将确保您的编译器(gcc)能够找到头文件。从您得到的错误消息中,我猜测它不是,但是您应该看到来自编译器的错误消息,关于无法找到该文件。另外,如果文件在您的系统上,检查是否有与git站点相比较的适当内容。

Finally, the remain errors that you are seeing should be fixed as well as they are all basically the same thing.

最后,你所看到的仍然错误应该是固定的,因为它们基本上是一样的。

#1


1  

as others have said, this is C code not C#.

正如其他人所说,这是C代码而不是c#。

Anyhow, you have two errors (and they do not relate directly) to the makefile but rather your compilation environment and the code itself.

无论如何,您有两个错误(它们并没有直接关联)到makefile,而是您的编译环境和代码本身。

OK, so how to approach something like this. First notice the first line in your screen-capture, that is the command that is being executed that is generating the error messages, I'll reproduce it here for you;

好,那么如何处理这样的问题。首先注意屏幕截图中的第一行,这是正在执行的命令生成错误消息,我将在这里为您复制它;

gcc -c -Wall -Werror -fpic -O2-D NDEBUG i2c_get.c i2c_set.c i2cbusses.c i2cset.c util.c

With this command we are compiling (note the -c flag) a bunch of source files into a single object file, the presence of the `c' flag implies that no linking is performed here. This is relevant so we know where along the tool-chain we are, and the type of errors we can expect (typically either syntax errors or missing header files).

通过这个命令,我们正在编译(注意-c标志)一堆源文件到一个单一的对象文件中,“c”标志的存在意味着这里不执行链接。这是相关的,因此我们知道我们在工具链上的位置,以及我们可以预期的错误类型(通常是语法错误或缺少头文件)。

The first error;

第一个错误;

i2cset.c: In function 'check_funcs'
i2cset.c:56:2 error: implicit declaration of function 'iotcl' [-Werror=implicit-function-declaration]

is kinda saying, "hey, I can see that ioctl is a function, but you haven't told me anything about it so I'm going to assume that its signature is int ioctl() — a function with an indeterminate (but fixed, not variadic) argument list that returns an int". Given that you are compiling on a Linux-based system, adding #include <sys/ioctl.h> to the top of the file should fix this error.

就是说,我可以看到ioctl是一个函数,但是你还没有告诉我它的任何信息,所以我要假设它的签名是int ioctl()——一个带有不确定(但是是固定的,不是可变的)参数列表的函数,它返回一个整数。考虑到您正在基于linux的系统进行编译,添加#include 应该修正这个错误。

The second error;

第二个错误;

i2cset.c:63:7: error: 'I2C_SMBUS_BYTE' undeclared (first use in this function)

is related to the first error; and it is kinda saying 'hey, you haven't told me anything about I2C_SMBUS_BYTE'. Again, the most common reason for seeing this error is a missing header file. Looking at the source files you've provided a link to, it seems that I2C_SMBUS_BYTE is defined in the header file i2c-dev.h, which however appears to be included in i2cset.c by: #include <linux/i2c-dev.h>.

与第一个错误有关;它会说,嘿,你没有告诉我I2C_SMBUS_BYTE的任何信息。同样,看到这个错误最常见的原因是缺少头文件。查看您提供的链接的源文件,看起来I2C_SMBUS_BYTE是在头文件i2c-dev中定义的。h,但是似乎包含在i2cset中。c:# include < linux / i2c-dev.h >。

At this point I'd insure that your compiler (gcc) can find the header file. From the error messages you are getting, I'm guessing that it is not, but you should be seeing an error message from the compiler about not being able to find the file. Also, if the file is on your system check to see if has the appropriate contents as compared to the git site.

在这一点上,我将确保您的编译器(gcc)能够找到头文件。从您得到的错误消息中,我猜测它不是,但是您应该看到来自编译器的错误消息,关于无法找到该文件。另外,如果文件在您的系统上,检查是否有与git站点相比较的适当内容。

Finally, the remain errors that you are seeing should be fixed as well as they are all basically the same thing.

最后,你所看到的仍然错误应该是固定的,因为它们基本上是一样的。