我在MacBook Air(13英寸,2011年中)上使用(OSX 10.10.5)编译NetBeans IDE 8.0.2(Build 201411181905),编译器版本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"
> ^
答案 0 :(得分:1)
正如您所看到的那样,文件位于不同的文件夹中,编译器无法读懂您的想法和意图:
stack/src/stack.c
stack/src/stack.h
stack/main/demo_stack.c?
首先,如果要将两个c文件链接在一起并生成可执行文件,则需要编译这两个c文件,因为带有main()函数的文件引用push
中声明的函数stack.h
但在stack.c
中定义。然后,您需要告诉编译器在编译主文件时应考虑包含stack.h
的路径。您的最终构建命令应该类似于
clang -c -I../stack demo_stack.c ../stack/stack.c