-2
#include<bits/stdc++.h>

using namespace std;

{
   // only run when we press ./a.out
   // instruction 1

}

{
  // only run when we press g++ main.cpp or gcc main.c
  // instruction 2
}



int main() {

    // other part of code

}

actually I want to read/write in a .txt file. when I run g++ main.cpp new .txt file should get created and when I run ./a.out " fout<<"my data"<<endl " should get execute.

  • This is not C code. Please don't tag it as such. – tadman Dec 28 '20 at 17:00
  • 3
    No C++ code is executed at compile time. It might be optimized or partially evaluated, but it is never executed. What problem are you trying to solve? – tadman Dec 28 '20 at 17:01
  • 6
    The proper place for such commands is in your Makefile or other build scripts. – Nate Eldredge Dec 28 '20 at 17:02
  • While there's limited support for compile time executing code, reading and writing files isn't part of it. You should look into an actual build system instead of trying to abuse the language. It's also a horrible design to mix build parts with application code. – Voo Dec 28 '20 at 17:03
  • 1
    What is the actual problem you need to solve? Why do you think you need to "read" a text file at compile-time? What is the purpose of this text file? What do you want to do with its contents? – Some programmer dude Dec 28 '20 at 17:03
  • before the compilation there is a source but no instruction by definition, anyway the compiler can compute expressions by itself if this is the question ... and use the result rather than generating code, example with `int i = 1+2;` the compiler will compute 3 ... except if the value is finaly useless of course – bruno Dec 28 '20 at 17:03
  • C++ does not work this way. – Sam Varshavchik Dec 28 '20 at 17:05
  • @tadman in fact, in C++, consteval and (some) constexpr functions are executed at compile-time. But you can think about them as being "optimized or partially evaluated" ;) – Bktero Dec 28 '20 at 17:06
  • 1
    @Bktero There's limits to what you can do there. I call it "partially evaluated" because you can't do everything, just a small subset of things. This is unlike Python or Ruby, for example, where everything is on the table during code execution. – tadman Dec 28 '20 at 17:08
  • 1
    @tadman you're right, this is quite limited (even if C++20 has widely increased the possibilities). I understand better what you mean by "partially evaluated" (even though technically code is executed). – Bktero Dec 28 '20 at 17:11
  • 1
    @Bktero Maybe future versions of C++ will allow executing arbitrary C++ code at the compilation phase so we can use more aggressive metaprogramming, but that day has not yet arrived. – tadman Dec 28 '20 at 17:14
  • 1
    I think we are looking at a https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem Please take one logical step back and describe what you want to achieve by creating a file during compilation time. Note that compiling is something which cannot be guaranteed to happen on the machine were the code is executed. many modern programs have the concept of installation prior to execution, maybe that is an option for you. Please explain why e.g. an "if not exit {create file}; rest of the program" approach is not an option for you. – Yunnosch Dec 28 '20 at 17:53

1 Answers1

1

The code you write describes the behavior of the program that will be the result of the process (what the program will do when executed).

What you want is part of the build process itself (what is done in addition to compilation / linkage / etc).

If you call g++ directly from the command-line, I guess you cannot achieve your goal. You have to use CMake (or make or any other build system). The exact way to do it depends on the build system.

yemre
  • 708
  • 3
  • 11
Bktero
  • 722
  • 5
  • 15