How do I split my c++ program into files that contain plain code lines, not necessarily functions?

230 views Asked by At

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 ?

2

There are 2 answers

5
Mark Adelsberger On

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
}
===========================================
4
Richard Hodges On

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

}