-1

this question looks familiar but trust me it actually is not ! Let me explain why. This is not about saving a function in a header file and including and calling it. An example will illustrate my query clearly so here we go: I have a program like the following:


#include whatever_should_be_included

int main() {

whatever-1

whatever-2

whatever-3

. . .

}


All those whatevers are codes in the program, including if conditions, loops and really whatever. I want those "whatever"s to be saved in files, let's say plain text files or whatever extension is necessary, let's say "exten" be the extensions, then I want to save them as:

chunk-1.exten, chunk-2.exten, .... etc files that will contain: whatever-1, whatever-2,... etc chunks of lines of codes and my program now should look like:


#include whatever_should_be_included #include those_chunks_maybe

int main(){

chunk-1.exten; //or whatever syntax necessary

chunk2.exten;

chunk-3.exten;

. . .

}


I am a beginner in C++ and in programming in general so please go easy on me :) a step by step clear answer with a bit of explanation will be really appreciated.

Edit-1:

  1. I am using Ubuntu 16.04

  2. My compiler is g++ although I am not directly using it, I am compiling or rather loading the programs inside the CERN's ROOT shell, as root macros.

Edit-2:

Of course a better way to go is to use functions and headers, but that does not mean that we cannot explore this feature of chunking out plain code-texts to different files and include them inside the main ! I wanted to learn this and I don't understand how learning this (however dull) feature is wrong !! Why voting negative exactly ? Is this question harming people ? Is it not curious and promote knowledge ?

quanta
  • 215
  • 3
  • 14

2 Answers2

3

I'll say a little about how you can, but first I'll say you probably shouldn't. I don't know your application and possibly you really do have a good reason, but this runs against the typical grain of the language and is more likely to cause problems with readability and maintenance than to solve whatever underlying concern is driving the question...

That said, in C++ directives that start with # are for a "pre-processor" which modifies the code prior to compilation. In particular #include copies the content of another file in place of the directive so everything gets compiled together. It is usually (and IMO most correctly) used to pull in headers, but it can be used for anything.

By convention you're correct in thinking that your "chunk" files should not end in .c, or .cpp, or .h; because those imply certain things about the structure of what's inside. You can make up an extension I guess, as long as it doesn't conflict with any standard.

So if you have chunk1.cfragment or something like that, and it contians your whatever-1, then the top-level .cpp file would look like

#include whatever_should_be_included

int main(){

#include "chunk1.cfragment";
#include "chunk2.cfragment";
#include "chunk3.cfragment";

. . .
}

I don't recall (and it may depend on your compiler) whether you can have whitespace at the start of the line before the #include directive; if not that's another way your code will end up ugly (but far from the worst problem with readability here).

Note that you use quotes for the included filename here, not angle brackets as you would with a system library.

UPDATE - Based on your comments on Robert's answer, it seems what you're really after is globally visible/modifiable variables. Again I'll say that this is not usually the best thing, but if you need it there are better ways to get it than by gluing together code fragments into a single massive function.

Better than creating global data, usually, is to pass parameters between functions as needed. But if you do have data that is needed literally everywhere, you can do it with global variables.

In general functions can share access to global variables - those declared outside of any function. To share them across code files, you need to provide extern declarations in all files (and the actual variable definition in exactly one code file). This is best managed with headers.

It's been a long time, so I may have some details a bit off here, but it would look more or less like this:

File1.h
===========================================
extern int a;
===========================================

File2.h
===========================================
void otherFunction();
===========================================

File1.cc
===========================================
#include "File1.h"
#include "File2.h"

int a;

int main() {
    otherFunction();
}
===========================================

File2.cc
===========================================
#include "File1.h"
#include "File2.h"

void otherFunction() {
    // do something with a
}
===========================================
Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • You have included the `chunk` files twice, once at the top as `#include those_chunks_maybe` and then inside the `main` program, and inside the main program you used again `#include` does this mean these (latter) inclusion of the files will act as kind of "**pasting**" in that exact position of the `main` ? – quanta Dec 23 '16 at 16:54
  • Yeah, well, copy-and-pasted your base code and didn't notice it. Will edit the includes... To your question, yes, `#include` puts the content of the file exactly in place of the `#include` directive itself. – Mark Adelsberger Dec 23 '16 at 18:10
  • Sorry but it's getting complicated for me!What I really needed is those "`.cfragments`" files to act like as if **pasting** their contents inside the `main` program in those specific positions and there should not be any problem with `variable definitions` as I could even define all the `variable`s inside another "`variables.cfragment`" file and `include` that in the beginning of the `main`. As I have **too many variables** inside those `chunks`, using them as `headers` or `functions` forces me to pass `arguments` and define `variables` inside functions, this is a really messy business for me. – quanta Dec 23 '16 at 19:43
  • Well, as messy a business as you think it is to follow C/C++ idioms for data sharing, if you ever maintain a program done the way you describe over a long period of time you'll find it becomes much worse. But don't apologize to me; you're creating your own headache, not mine. – Mark Adelsberger Dec 23 '16 at 20:02
  • Ok ! That's it, tried my **beast**.. didn't work, I am gonna follow your procedure.. so function/header it is ! – quanta Dec 23 '16 at 23:06
1

Even if you're thinking in terms of "code fragments", you will have a happier time if you express them as functions.

file: whatever1.hpp

#pragma once

inline whatever1(int& arg)
{
   // code
}

file: main.cpp

#include <iostream> // say...
#include "whatever1.hpp"


int main() {

    int some_variable;

    whatever1(some_variable);

    // ... etc

}
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • I did exactly as you said but there seems to be a problem, the variables defined in the main program are not recognized inside those "`whatever`" functions. The compiler complains: `symbol `blah` is not defined in current scope whatevern.hpp:8`. – quanta Dec 23 '16 at 18:13
  • pass references to the variables as arguments... will update – Richard Hodges Dec 23 '16 at 18:14
  • Sorry, but in one chunk I have too many variables, passing them as arguments would be a headache for me ! I really wanted those `chunk` files as fragments in real sense, like as if they just paste themselves in the main code ! – quanta Dec 23 '16 at 19:33
  • @quanta I wish you best of luck testing and maintaining your demon spawn. – Richard Hodges Dec 23 '16 at 19:35