NO

Author Topic: #include and source files  (Read 7519 times)

ohmycee

  • Guest
#include and source files
« on: July 25, 2011, 09:00:00 PM »
Hi,

I have 2 questions that are confusing me right now.

1st question:
Right now, I'm working on 4 projects. They all use the SQLite amalgamation source. The question I have is, how come some example source I see looks like this:

Code: [Select]
#include "sqlite3.h"
while I've also come across

Code: [Select]
#include <sqlite3.h>
How do I get them all to be #include <sqlite3.h>?

2nd Question:
Right now, I have 4 copies of sqlite source code in 4 separate directories; one for each project. In each of these directories I have an output folder and sqlite3.obj that takes super long to compile. How to I arrange things so that I have only one copy of the sqlite source code and one copy of sqlite3.obj in one directory only?

I have a feeling both questions are somewhat related, but I don't know how to make it happen or what the process is called. Please help? Thanks!

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: #include and source files
« Reply #1 on: July 25, 2011, 09:22:33 PM »
Ad 1)
The double quotes refer to a local header file included only in the project folder.
The less and greater-than format refers to a global header file accessed through the include path setting of the compiler/linker.

Ad 2)
Put the SQLite source in a separate folder and compile a .LIB file out of it.
Now you can include the header and .LIB file using the project settings for the include folders.
---
Stefan

Proud member of the UltraDefrag Development Team

ohmycee

  • Guest
Re: #include and source files
« Reply #2 on: July 25, 2011, 09:54:02 PM »
Thanks Stefan Pendl.

That solves one of my problem. Now I got another question. What if something I'm using contains multiple .c files (about 20)? Do I create a .lib for every one of them? Is there a quick way to do this?

Thanks again!

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: #include and source files
« Reply #3 on: July 25, 2011, 10:17:14 PM »
The amount of source files does not matter.

If you have a library of functions you have two ways to include it:
  • Use a header and a LIB file to link to the functions internally.
  • Use a header, a LIB file and a DLL to link to the functions externally and add the DLL to the distribution.
The first option requires you to only create a static LIB file.
The contents of the LIB file will be linked into your main projects executables, which makes them bigger.

The second option requires you to create a static LIB file and a DLL.
This keeps the main project executables small, since they load the functions from the DLL.



Which option you use depends on the way you like to distribute the applications depending on the common code.

Should it be possible to have the common DLL shared or should the applications be independent from each other.
---
Stefan

Proud member of the UltraDefrag Development Team

ohmycee

  • Guest
Re: #include and source files
« Reply #4 on: July 25, 2011, 10:42:06 PM »
Ooohhhh... I get it now. Thank you so much!

CommonTater

  • Guest
Re: #include and source files
« Reply #5 on: July 25, 2011, 11:34:09 PM »
Thanks Stefan Pendl.

That solves one of my problem. Now I got another question. What if something I'm using contains multiple .c files (about 20)? Do I create a .lib for every one of them? Is there a quick way to do this?

Thanks again!

All you need to do for C files in the same project is create a .h with function prototypes, extern variables etc. and include it into your other .c files using the "header.h" format.
For example...
Code: [Select]
#include <windows.h>   // global includes
#include <stdio.h>

#include "Files.h"          // project includes
#include "Network.h"

// etc.

« Last Edit: July 25, 2011, 11:38:20 PM by CommonTater »

ohmycee

  • Guest
Re: #include and source files
« Reply #6 on: July 26, 2011, 05:44:04 AM »
Thanks, CommonTater.

Now, I've got another noob question. How come when I do:

Code: [Select]
#include <stdio.h>
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>

... I don't have to point the program anywhere else, and it seems to know what to do to find the relevant libraries, whereas, when I do this:

Code: [Select]
#include <myown.h>
My program gets broken, even though I've added the folder my .lib file is in into the project options. My program only works when I do this:

Code: [Select]
#pragma lib "myown.lib"
Is this how it's done, or is there another "professional, non noob" way of doing this?

Thanks all!

CommonTater

  • Guest
Re: #include and source files
« Reply #7 on: July 26, 2011, 03:10:40 PM »
Thanks, CommonTater.

Now, I've got another noob question. How come when I do:

Code: [Select]
#include <stdio.h>
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>

... I don't have to point the program anywhere else, and it seems to know what to do to find the relevant libraries,

Those are not libraries they're "headers" or Include Files...   It finds them because their folders are listed in

Tools -> Options -> Folders -> Include

Quote
whereas, when I do this:

Code: [Select]
#include <myown.h>

That should be  #include "myown.h" since the file is in your project folder...

"" is for local include files, in the same folder as your source code
<> is for include files on the search path (as above)

Quote
My program gets broken, even though I've added the folder my .lib file is in into the project options. My program only works when I do this:

Code: [Select]
#pragma lib "myown.lib"
Is this how it's done, or is there another "professional, non noob" way of doing this?

Thanks all!

You should use the $pragma lib  only when including libraries (*.lib). 

Usually when writing a C program with multiple source files one is not making libraries.  C Source files compile to objects, not libraries, in the project\output folder and the linker knows to find them there.  The include (header) files are used to alert the compiler to a function or variable in a different source file.

For example...
Code: [Select]
// main.c
#include <stdio.h>      // header on search path
#include "domath.h"   // header in local directory

int main (void)
  { int x = 21;
     int y = 0;
     
     printf(" x = %d\t y = %d\n", x, y);
 
    // call function in different source file
     y =  timestwo(x);

     printf(" x = %d\t y = %d\n", x, y);

     return 0; }


// domath.h
#ifndef DOMATH_H     // include guard
#define DOMATH_H    // prevents multiple inclusions

int timestwo(int var);   // function prototype

#endif  // include guard



// domath.c
int timestwo(int var)
  { return var * 2; }

While this is an extremely trivial example, it should give you the general idea about how these things are set up.

Hope this helps.



ohmycee

  • Guest
Re: #include and source files
« Reply #8 on: July 26, 2011, 03:19:46 PM »
Yes, Thanks!

It's a good simple example to get me started in the right track.

CommonTater

  • Guest
Re: #include and source files
« Reply #9 on: July 26, 2011, 04:16:01 PM »
Yes, Thanks!

It's a good simple example to get me started in the right track.

You may want to cruise around the AddIns and User Contributions sections of the forum.  I and many others have uploaded all kinds of code there.  Take a look at the way we set up our files and headers... You can study our work, see how things get done, even see our various programming styles.  Hopefully that will help you understand this better.

FWIW... When I first transitioned over from Pascal to C it wasn't pointers that tripped me up (like most people), it was the relationships between headers, source, and code that I went "splat" over.  In Pascal there is a one to one relationship between "headers" (called "units") and source code files... not so in C, so it took me a bit to get my head around it. 

Glad I could help...