C - include + define - I have a question.

Hi all,
Said me please..

I have next directory, for ehample: /usr/home/iam/work. In this folder:
a2.h
Code:
#ifndef _A2_H_
#define _A2_H_

int
summ(int a, int b, int c)
{
    return a+b+c;
}

#endif
abc.c
Code:
#include <stdio.h>
#include "/usr/home/iam/work/a2.h"

int
main(int argc, char *argv[])
{
    extern int summ(int, int, int);
    printf("Result: %d\n", summ(1,2,3));

    return 0;
}
If I compile abc.c, etc. cc -o run abc.c - all ok. I know what I can be use next style, for include local modules:
#include "a2.h"
- not write full path to it. But me need it. I have my private C library. And I thought, what if i write use next style:
abc.c
Code:
#define loadit(name) "/usr/home/iam/work/"#name
#include <stdio.h>
#include loadit(a2.h)

...

But if I compile it - I have:
abc.c:12:23: error: /usr/home/iam/work/: No such file or directory

I understand, as my (not my, C has) preprocessor use this script:
Step one: it saw 3 line [ #include loadit(a2.h) ], it search define loadit.
Step two: it rewrite line 3 to next style:
#include "/usr/home/iam/work/" "a2.h"
After preprocessor tries to run #include command. But It don't know file name: "/usr/home/iam/work/" "a2.h" it wants to see it as: "/usr/home/iam/work/a2.h".

I even tried to do so:
abc.c
Code:
#define loadit(name) /usr/home/iam/work/ ## name
#include <stdio.h>
#include loadit(a2.h)

...

But #include style should has double quotes. :(

If I use
Code:
#define loadit2(name) <std ## name>
#include loadit2(io.h)

- all ok. This works for standard libraries.

And how do you connect your own library?
 
Less than a minute. :)

Code:
#define loadit(name) </usr/home/iam/work/ ## name>
#include loadit(a2.h)

- all ok.
It's settled!
I Love C - thank you Dennis Ritchie!

But I will not close the topic - I wonder how you connect your library?

Added ...
I hurried to conclusions.
I checked as follows:
#include </usr/home/iam/work/a2.h>

And it worked, then I wrote this post.
But in fact it does not work:
Code:
#define loadit(name) </usr/home/iam/work/ ## name>
#include loadit(/a2.h)

Error:
abc.c:8:1: error: pasting "/" and "a2" does not give a valid preprocessing token
Sorry me.
 
Add the additional header library to gcc.

[cmd=]cc -I/usr/home/iad/work -o run abc.c[/cmd]
 
O, it is really perfect! Thank you! Really perfect. Previously, I used WIndows OS - and still do not know much opportunity cc/g++.
It's easy and it's all settled! Thanks!
 
Alt said:
Instead #define (or with them) you can set
Code:
#pragma once
- most compilers support it

Sorry, but is it does not apply to my question!
#pragma once
it is alternative
file.h
Code:
#ifndef _FILE_H_
#define _FILE_H_

/* code */
#endif

But me needed to dynamically generate a path to my library. User SirDice gave an exhaustive answer.

But thank you too. Style #pragma once accelerates compilation and reduces the number of #define definitions.
But I still like it "Include guard".
 
Back
Top