# C - include + define - I have a question.



## valsorym (Dec 7, 2011)

Hi all,
Said me please..

I have next directory, for ehample: /usr/home/iam/work. In this folder:
a2.h

```
#ifndef _A2_H_
#define _A2_H_

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

#endif
```
abc.c

```
#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 

```
#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

```
#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

```
#define loadit2(name) <std ## name>
#include loadit2(io.h)
```

- all ok.  This works for standard libraries.

And how do you connect your own library?


----------



## valsorym (Dec 7, 2011)

Less than a minute. 


```
#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:

```
#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.


----------



## SirDice (Dec 7, 2011)

Add the additional header library to gcc.

[cmd=]cc -I/usr/home/iad/work -o run abc.c[/cmd]


----------



## valsorym (Dec 7, 2011)

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 (Dec 7, 2011)

Instead #define (or with them) you can set 
	
	



```
#pragma once
```
 - most compilers support it


----------



## valsorym (Dec 7, 2011)

Alt said:
			
		

> Instead #define (or with them) you can set
> 
> 
> 
> ...



Sorry, but is it does not apply to my question! 
`#pragma once`
it is alternative 
file.h

```
#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".


----------

