由于包含问题而无法编译C代码

时间:2018-10-22 09:50:57

标签: c ubuntu makefile include

我有一个要使用硬件API(RTL SDR)的c代码,下面是代码示例:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <string.h>
#include "/home/user/rtl-sdr/src/convenience/convenience.h"
#include "/home/user/rtl-sdr/include/rtl-sdr.h"

int main(int argc, char *argv[])
{
   int dev_index = 0;
   dev_index = verbose_device_search("0");
   return 0;
}

这是我使用的makefile:

CC=gcc
CFLAGS= -O0 -c -Wall
LDFLAGS= 

SOURCES=$ fm_rx.c
OBJECTS=$(SOURCES:.c=.o)

EXECUTABLE=fm_rx

all: $(TASKMAP) $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@ -lm

.c.o:
    $(CC) $(CFLAGS) $< -lm -o $@

clean: 
    rm -fr $(OBJECTS) $(EXECUTABLE)

每次编译时,都会出现此类错误:

make all 
gcc -O0 -c -Wall fm_rx.c -lm -o fm_rx.o
In file included from fm_rx.c:6:0:
/home/user/rtl-sdr/src/convenience/convenience.h:55:18: error: unknown type name ‘rtlsdr_dev_t’; did you mean ‘__dev_t’?
 int nearest_gain(rtlsdr_dev_t *dev, int target_gain);
                  ^~~~~~~~~~~~
                  __dev_t
/home/user/rtl-sdr/src/convenience/convenience.h:65:27: error: unknown type name ‘rtlsdr_dev_t’; did you mean ‘__dev_t’?
 int verbose_set_frequency(rtlsdr_dev_t *dev, uint32_t frequency);
                           ^~~~~~~~~~~~
                           __dev_t
/home/user/rtl-sdr/src/convenience/convenience.h:75:29: error: unknown type name ‘rtlsdr_dev_t’; did you mean ‘__dev_t’?
 int verbose_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate);
                             ^~~~~~~~~~~~
                             __dev_t
/home/user/rtl-sdr/src/convenience/convenience.h:85:29: error: unknown type name ‘rtlsdr_dev_t’; did you mean ‘__dev_t’?
 int verbose_direct_sampling(rtlsdr_dev_t *dev, int on);
                             ^~~~~~~~~~~~
                             __dev_t
Makefile:16: recipe for target 'fm_rx.o' failed
/home/user/rtl-sdr/src/convenience/convenience.h:94:27: error: unknown type name ‘rtlsdr_dev_t’; did you mean ‘__dev_t’?
 int verbose_offset_tuning(rtlsdr_dev_t *dev);
                           ^~~~~~~~~~~~
                           __dev_t
.
.
.
fm_rx.c: In function ‘main’:
fm_rx.c:12:8: warning: variable ‘dev_index’ set but not used [-Wunused-but-set-variable]
    int dev_index = 0;
        ^~~~~~~~~
make: *** [fm_rx.o] Error 1

编辑

这是目录结构 anouar @ anouar-Lenovo-IdeaPad-Y510P:〜/ rtl-sdr $ tree -d

.
├── build
│   ├── CMakeFiles
│   │   ├── 3.10.2
│   │   │   └── CompilerIdC
│   │   │       └── tmp
│   │   ├── CMakeTmp
│   │   └── uninstall.dir
│   ├── include
│   │   └── CMakeFiles
│   └── src
│       └── CMakeFiles
│           ├── convenience_static.dir
│           │   └── convenience
│           ├── rtl_adsb.dir
│           ├── rtl_eeprom.dir
│           ├── rtl_fm.dir
│           ├── rtl_power.dir
│           ├── rtl_sdr.dir
│           ├── rtlsdr_shared.dir
│           ├── rtlsdr_static.dir
│           ├── rtl_tcp.dir
│           └── rtl_test.dir
├── cmake
│   └── Modules
├── contrib
├── debian
│   └── source
├── include
├── m4
└── src
    ├── convenience
    └── getopt

我知道包含文件和librtlsdr.a文件的路径,但是我不知道如何包括它们……有人可以告诉我怎么做吗?

1 个答案:

答案 0 :(得分:0)

放置绝对路径以在代码中包含文件并不是一个好主意。我建议您在代码中仅保留包含文件名或路径的最后一部分:

...
#include <string.h>
#include "convenience/convenience.h"
#include "rtl-sdr.h"
...

要帮助编译器找到那些文件,应使用-I标志。例如,对于先前的文件,您可以在Makefile中使用此CFLAGS:

CFLAGS= -O0 -c -Wall -I/home/user/rtl-sdr/src -I/home/user/rtl-sdr/include

这样,您可以使代码不依赖于库的路径。完成此操作后,您将必须查找其中声明为缺少类型的包含。您可以这样做:

grep -r rtlsdr_dev_t /home/user/rtl-sdr

出口将显示使用该类型的位置以及声明该类型的位置。

一旦找到声明该文件的文件,则将相应的include添加到您的代码中,并在需要时在Makefile中的CFLAGS中添加相应的-I路径。

编辑:

您所看到的确切问题是由于在convenience.h在rtl-sdr.h中定义类型之前使用了rtlsdr_dev_t类型引起的。将rtl-sdr.h包含在convenience.h之前。