0

I have a very long script with many function and global parameters, I wish to work with three separate files: main script, functions and parameters.

I've seen lately someone includes a global variable file into a cpp script without header files. All the answer here show how to do it with a .cpp and .h file, something I find to be cumbersome. Is there a way to make this simple example work using a single file?

// param.cpp - global parameters separate file
double a=1.7;
double g=8/2;

// main.cpp - main script file
#include <iostream>
#include <math.h>
#include "param.cpp"
using namespace std;
int main()
{
  double d = a+g;
   return 0;
}
jarhead
  • 1,821
  • 4
  • 26
  • 46
  • @Starl1ght , this is not a duplicate, I don't want to share it between different source files externally, read the questions – jarhead Aug 18 '16 at 17:30
  • Why not put it all in a header file? – wally Aug 18 '16 at 17:31
  • 1
    In this question, what do you mean by 'script'? – wally Aug 18 '16 at 17:33
  • 1
    Well you can do it yourself by writing `extern double a;...` in the files you want to share the global variable with but that is exactly what the header file does. Using the header file cuts down on typing and mistakes. – NathanOliver Aug 18 '16 at 17:34
  • @nathanOliver. but then I'll need to files, param.cpp and param.h, right? – jarhead Aug 18 '16 at 17:37
  • @flatmouse I assume they mean "a small program that runs as part of another process". Like, what you'd usually do with Python – KABoissonneault Aug 18 '16 at 17:38
  • @jarhead No you don't need a .h but you are just manually doing what having and including the .h file does. Why do something manually when you can have it done automatically? – NathanOliver Aug 18 '16 at 17:40
  • @KABoissonneault How would you run c++ like you usually do with Python? – wally Aug 18 '16 at 17:44
  • @NathanOliver, I don't think I got what you wrote, I would like to assign them a value in the .h/.cpp file and then include it – jarhead Aug 18 '16 at 17:48
  • And you can do that. You have your defenition in param.cpp and then in all of your other cpp files you need to manually write `extern double a;extern double a;...` so you can share them. Doing this though is just doing what a header file would automatically do for you. Something like [this](http://melpon.org/wandbox/permlink/lzwYTlbo67V9M7RZ) – NathanOliver Aug 18 '16 at 18:03

0 Answers0