简体   繁体   English

在标头文件中包含标头不会使它包含在实现文件中-还是我只是使用错误的命令进行编译?

[英]Including headers in header file doesn't make it included in implementation file - or am I just using wrong command to compile?

Well, I have a header (my_prog.h) which looks like this: 好吧,我有一个标头(my_prog.h),看起来像这样:

#ifndef __MY_HEADER_H
#define __MY_HEADER_H
#include <stddef.h>
typedef struct {
    size_t something;
    size_t something_else;
}
void my_func();
#endif

and implementation file (my_prog.c) where I put: 和我放在其中的实现文件(my_prog.c):

#include "my_prog.h"
static size_t min(size_t a, size_t b) {...}
void my_func() {...}

When I try to compile my_prog.c to object file (I need it for linking with other files) I fet: 当我尝试将my_prog.c编译为目标文件(与其他文件链接时需要它)时,我感到:

error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘min’

The command I use for compiling is: 我用于编译的命令是:

gcc -c my_prog.c -o my_prog.h

There's no error saying that it couldn't find the source. 没有错误说它找不到源。 When I include in implementation file it compiles wihtout issues. 当我在实现文件中包含它时,它将编译所有问题。

  1. Remove the ... from the function body. 从功能体上拆下... Having them is a syntax error. 拥有它们是语法错误。

  2. You have not given a typedef name to the structure and the ; 您尚未为该结构和the赋予typedef名称; is missing: 不见了:

     typedef struct { size_t something; size_t something_else; } foo; ^^^^ 
  3. In the compile line, following the -o you are specifying the name of your header file. 在编译行中,在-o您将指定头文件的名称。 This is incorrect. 这是不正确的。 If the compilation goes fine(it will if you fix 1 and 2 above) , the compiler the wipe the original contents of my_prog.h and will overwrite it with the object file. 如果编译正常(如果您修复上面的1和2,它将进行编译),编译器将擦除my_prog.h的原始内容, my_prog.h其用目标文件覆盖。 Instead do : 而是:

     gcc -c my_prog.c -o my_prog.o 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在编译命令中包含头文件时出现编译错误 - Compilation error while including header file in compile command 使用make包含头文件时出错 - Error while including header file using make Pure C: when using typdef void StructName in header file, the header file can't be included into the.c file - Pure C: when using typdef void StructName in header file, the header file can't be included into the .c file 如果没有使用头文件中包含的函数,编译器是否仍然编译此文件? - If no functions included in a header file are used, does the compiler still compile this file? 为什么在我的C头文件中包含freetype头文件会产生错误,但可以包含在.c文件中? - Why does including the freetype header files in my C header file produce an error, but can be included in the .c file? 为什么我不能使用VC ++中预编译的头文件中包含的头文件中的typedef? - Why can't I use a typedef from a header file which is included in a precompiled header in VC++? 制作 header 文件,编译并在 C 中运行 - Make header file, compile them and run in C 包括已知已经包含在内的文件 - Including file known to already be included 我已经包含了头文件,但是在尝试运行make时仍然无法定义 - I've included a header file but I still get undefined when trying to run make 使用C struct而不包含头文件 - Using C struct without including header file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM