Technology Sep 05, 2026 · 2 min read

No one asked, but I've written a C++ header-only library so you don't have to declare the variable again.

In my spare time, I wrote a header-only C++ library that will help you extract variable declarations from the main(...) function of one .cpp file to the main(...) function of another .cpp file. Yes, I mean you can declare any type of variable in any way you can think of, as long as it's within the m...

DE
DEV Community
by Nguyen Duc Minh Quan
No one asked, but I've written a C++ header-only library so you don't have to declare the variable again.

In my spare time, I wrote a header-only C++ library that will help you extract variable declarations from the main(...) function of one .cpp file to the main(...) function of another .cpp file.
Yes, I mean you can declare any type of variable in any way you can think of, as long as it's within the main(...) function, from a custom-named struct to strange declarations like int a,b,c = 100; as long as it's syntax-correct in C++.

Quick Example :

#include<iostream>
#include<vector>
#include "var_saver.hpp"
int main(){
    int a = 0;
    std::vector<int>c;
    double f,r=3,h;
    //Save variables to a file 
    VarSaver::SaveVarToFile(__FILE__,"save.out");//or another name
    // Load the variables into target file  
    VarSaver::LoadVarToFile("save.out","main2.cpp");
    return 0;
}

main2.cpp before loading the variables:

#include<iostream>
#include<vector>
int main(){
    return 0;
}

main2.cpp after loading the variables:

#include<iostream>
#include<vector>
int main(){
    int a=0;
    std::vector<int>c;
    double f;
    double r=3;
    double h;
    return 0;
}

Key advantage : You only need to call the function once—anywhere in your code, as long as it gets executed at least once during runtime.
Anyway, you can visit, check out, and try out this library.
Note: If you can't read the code in the repository, it's my fault, not yours. I'm a competitive programmer, so you might have some difficulty reading variable names and trying to understand their meaning.
Link repository: VarSaver
I'd love to hear your feedback and suggestions!

DE
Source

This article was originally published by DEV Community and written by Nguyen Duc Minh Quan.

Read original article on DEV Community
Back to Discover

Reading List