#include from a subdirectory

C

copx

How do I include headers which are in a sub directory (not in my main
src dir)?
I use a scripting language - Lua - to extend my C app and all the Lua
code is in the subdirectory \lua. I want to include the lua.h header in
one of my main source files but

#include ".\lua\lua.h"

doesn't work...

I've just tried it on Windows but the app is supposed to be portable to
Linux so there's also the "/" vs "\" issue..

copx
 
K

Karthik Kumar

copx said:
How do I include headers which are in a sub directory (not in my main
src dir)?
I use a scripting language - Lua - to extend my C app and all the Lua
code is in the subdirectory \lua. I want to include the lua.h header in
one of my main source files but

#include ".\lua\lua.h"

doesn't work...

As a matter of coding style you would put that in the makefile
and pass as arguments to the search directories of your compiler.
Your code might look as -

#include "lua.h"

..
..

And you might pass the include directories to the compiler.
say, if you are using gcc - it might be -
gcc -I ./lua yourprog.c

This syntax entirely depends on the implementation and the platform.
Refer to the manual to know this.
I've just tried it on Windows but the app is supposed to be portable to
Linux so there's also the "/" vs "\" issue..

As a convention, if you are writing portable apps, you have a separate
makefile specific to each implementation in which you specify all these
things. Of course, you can also tools like Ant, but i have seen people
using for java programs and not many people use Ant for building C
programs.

HTH.
 
J

James McIninch

#include "lua/lua.h"

.... for both Linux and Windows. Keep in mind the '\' indicates an escape
sequence (think '\n', '\r', '\0' and so on).
 
K

Keith Thompson

James McIninch said:
#include "lua/lua.h"

... for both Linux and Windows. Keep in mind the '\' indicates an escape
sequence (think '\n', '\r', '\0' and so on).

Please don't top-post. Your response goes below the quoted text.

The '\' doesn't necessarily indicate an escape sequence in a header
name. Even though it's delimited by '"' characters, the stuff
following the "#include" is not actually a string literal token.

C99 6.4.7 says that if a '\' character occurs between the "" or <>
delimiters in an include directive, the behavior is undefined (though
an implementation is free to define the behavior if it chooses to do
so).

Consult the documentation for your compiler.
 
J

Joe Wright

copx said:
How do I include headers which are in a sub directory (not in my main
src dir)?
I use a scripting language - Lua - to extend my C app and all the Lua
code is in the subdirectory \lua. I want to include the lua.h header in
one of my main source files but

#include ".\lua\lua.h"

doesn't work...

I've just tried it on Windows but the app is supposed to be portable to
Linux so there's also the "/" vs "\" issue..

copx

#include "lua/lua.h"

MSDOS (not command.com) since 3.0 and all versions of Windows know
how to treat '/' in a file specification.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top