Author Topic: including headers in files, not in project  (Read 5371 times)

gusfoca

  • Guest
including headers in files, not in project
« on: April 13, 2011, 03:07:51 am »
I've created some general-purpose classes I'd like to include in different projects. Problem is, it seems I have to include their files in every new project, which is undesirable, as I'm not going to edit them anymore. If I just set the search paths in the Build Options and add an "#include" on any file, it is considered undefined at compilation (it can't find the .cpp one, I suppose).

Is there a way I can #include those files in others but not in the project itself? Is it the case to build a library for them?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: including headers in files, not in project
« Reply #1 on: April 13, 2011, 06:32:06 am »
Is it the case to build a library for them?
Yes, use a library.

However, You'll always need to include the header file that describes the interface of the library. And there is no other solution possible. The compiler needs to know about interfaces, it surely cannot "guess". ;-)
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline Justin Brimm

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: including headers in files, not in project
« Reply #2 on: April 14, 2011, 02:26:29 am »
The other option is to move those headers into your compilers main include folder, or some other place you wish to put included headers to be shared among all projects, and then modify the compiler settings you're using to always have that folder in the search path. That way you don't need to do this more than once.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
Re: including headers in files, not in project
« Reply #3 on: April 14, 2011, 08:44:10 am »
Justin Brimm:
And what happens when you try to compile the project on different computer, os or compiler?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Justin Brimm

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: including headers in files, not in project
« Reply #4 on: April 16, 2011, 02:34:05 am »
Well, any compiler settings you change will be unique to that copy of Codeblocks, unless you purposefully copy over your Codeblocks settings to the new computer/system or modify the settings on the new computer to match the settings you changed on your original version of Codeblocks (i.e. you'll need to add that path to the default search directory settings again). You'll also need to copy the headers into the directory on the new computer/system as well.

No matter what compiler or IDE you use, there is no way to get around this. Compilers must have the header files available during the linking stage of building an application. Building a library makes it so you no longer need to haul around all the source or object files (also hiding your source code in the process), but you'll still need to provide the appropriate headers, or the compiler will have no way to properly build the application during the linking stage.