简体   繁体   中英

Unable to compile C Program

I am working on NetBeans IDE 8.0.2 (Build 201411181905) on MacBook Air (13-inch, Mid 2011) with (OSX 10.10.5) with Compiler version Apple LLVM version 7.0.0 (clang-700.0.72) /Target: x86_64-apple-darwin14.5.0.

I want to compile following codes: stack/main

#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);
}

stack/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}; 
}

stack/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_ */

When I run this statement:

gcc -c demo_stack.c

I get this error:

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

As you can see the files are in different folders and a compiler can't read your mind and your intents:

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

First of all if you need to compile both c files if you want to link them together and produce an executable, since the file with main() function refers function push which is declared in stack.h but defines in stack.c . Then you need to tell your compiler that the path that includes stack.h should be considered when compiling your main file. Your final build command should look something like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM