c - 无法编译C程序

标签 c macos gcc netbeans compiler-errors

我正在 MacBook Air(13 英寸,2011 年中)上使用 NetBeans IDE 8.0.2(内部版本 201411181905)(OSX 10.10.5),编译器版本为 Apple LLVM 版本 7.0.0 (clang-700.0.72)/目标:x86_64-apple-darwin14.5.0。

我想编译以下代码: 堆栈/主函数

#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
/*
 * 
  */
int main(int argc, char** argv) {

push(1.2);
(void)printf("On Stack");


//return (EXIT_SUCCESS);
}

堆栈/src/stack.c

    #include <stdio.h>
#include "stack.h"

/* initialize stack for float values */
static float stack[STACK_LENGTH] = { 0.0 };

/* 
 * increase stack and push new float into stack[0]
 * params:
 *  float > new value to push to position 0
 * return:
 *  void
 */
void push(float new) {
    /* to do */
    if(pos<STACK_LENGTH){
        stack[pos++]=new;
    }else{
        (void)printf("Stack-Overflow\n");
    }
}

/* 
 * pop float at position 0 and decrease stack 
 * params: void
 * return: float > value at position 0
 */
float pop(void) {
    /* to do */
    if(pos>0){
        stack[pos--];
    } else{
        (void)printf("Stack-leak\n");
    }
}

/* 
 * get stack at pos 
 * params: int > stack position 
 * return: float > value at position pos
 */
float get(int pos) {
    /* to do */
    float value;
    return value = stack[pos];
}

/* 
 * set float value in stack at pos
 * params:
 *  float > value to set at position pos
 *  int > stack position 
 * return:
 *  void
 */
void set(float value, int pos) {
    /* to do */
    stack[pos] = value;
}

/* 
 * list stack to console
 * params:
 *  void 
 * return:
 *  int > number of characters printed
 */
int list(void) {
    /* to do */
    return 0;
}

/* 
 * clear stack
 * params: void
 * return: void 
 */
void clear(void) {
    /* to do */
    float stack[15]=  {0.0}; 
}

堆栈/src/stack.h

#ifndef STACK_H_
#define STACK_H_

/* Includes */
#include <stdio.h>
#include <stdlib.h>

/* stack size */
#define STACK_LENGTH 15

/* global variables*/
static float stack[STACK_LENGTH];
static int pos = 0;

/*function declaration*/
void push(float);
float pop();
float get(int);
void set(float, int);
int list();
void clear();



#endif /* STACK_H_ */

当我运行此语句时:

gcc -c demo_stack.c

我收到此错误:

>fatal error: 'stack.h' file not found
>#include "stack.h"
>         ^

最佳答案

正如您所看到的,这些文件位于不同的文件夹中,编译器无法读取您的想法和意图:

stack/src/stack.c
stack/src/stack.h
stack/main/demo_stack.c?

首先,如果您想将两个c文件链接在一起并生成可执行文件,则需要编译它们,因为带有main()函数的文件引用了在中声明的函数push >stack.h 但在 stack.c 中定义。然后,您需要告诉编译器在编译主文件时应考虑包含 stack.h 的路径。您的最终构建命令应该类似于

clang -c -I../stack demo_stack.c ../stack/stack.c

关于c - 无法编译C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33200438/

相关文章:

c - "gcc -o test test.c; test"不产生任何输出

c - 从 C 中的文件中读取 int 或 float

c - 使用 lseek() 系统调用重置文件位置

macos - 如果开发者的分发证书过期,Mac 应用程序会发生什么情况?

java - 从 java 运行终端命令(使用 Eclipse)

检查 C 库的版本(动态加载)

gcc - gcc/clang 编译器选项中的 -f 和 -m 代表什么

c - 优化浮点计算

swift - Cocoa中的自定义View在设置AutoLayout Constraints后无法绘制

linux - Yocto 构建在 gcc 和硬 fpu 问题上崩溃