开发者

C compilation problem

I am learning how to use cygwin for alchemy. I created a test library celconv.h which is there inside the folder celconv. I want to use that header file in my c code.

When i use #include <celconv/celconv.h> the compiler gives an error "No such file or directory"

i compile the code like this:

gcc -c test.c

Test.c inside a folder named test

#include <stdio.h>
#include <celconv/celconv.h>

int main()
{
    float UserInput;

    printf("Enter a temperature in celsius to convert to  fahrenheit:");
    scanf("%f",&UserInput);
    celconvert(UserInput);

    return 0;
}

celconv.h inside a folder celconv which is inside test folder:

extern void celconv(float Cel);

celconv.c:

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

void celconvert(float Cel)
{

    float Fah;

    Fah = Cel * 9/5 + 32;
    printf("%f",Fah);开发者_开发知识库
}


Is the path you are giving correct? Be careful about using < > and " ". The first tells the compiler "search for include files in your include folder", while the second says "look into source files directory".


If the "celconv" directory is in the current directory, the best way is probably use #include "celconv/celconv.h" syntax. If it is somewhere else, then do this:

gcc -I/path/to/wherever/celconv/directory/is -c test.c

Note that it's not the path to the celconv directory itself, but to the containing directory!

As mentioned in the comment, this approach will work for the current directory too if you don't want to use "celconv/celconv.h" for whatever reason:

gcc -I. -c test.c


Include files in the local directory need to be referenced using the #include "file.h" syntax. The angle brackets do not look into the current directory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜