Is there a way to compile and run a single .cpp file in a Visual Studio Express '12 project?

I just started learning C ++, and I'm using Microsoft Visual Studio Express 2012. I started a project where I planned to have all my .cpp files, but now I have a problem when I try to compile and run a specific .cpp file, it doesnโ€™t works.

VS seems to just compile and run the .cpp file with the main function in it, and it creates .exe and runs it. Since my first .cpp file (which contains main ()) is a simple hello world program, I only get this when I try to compile and run now.

I have another .cpp file with an int age () function, which should ask the user's age and then output it. It is very simple, and I just want to run it to see it in action, but I canโ€™t figure out how to compile this particular .cpp file in my project, as it seems like it just wants to compile the main .cpp file with the main ( )

How can I compile a specific .cpp in a project?

+4
source share
2 answers

All C ++ programs start with the main function. Why don't you try calling age() from main ?

Of course, for this you will need your main.cpp to be aware that there is a function called age . Header files appear here.

In summary, you will need the following:

main.cpp

 #include "age.h" int main() { age(); return 0; } 

age.h

 #ifndef AGE_H #define AGE_H int age(); #endif 

age.cpp

 #include "age.h" int age() { // Do age stuff. return 42; } 
+2
source

Try splitting your .cpp files into projects if you really need to compile them separately. but for this you will also need the main thing in each of the projects.

Another choice you create is dll projects. But since you said that you want to keep it simple, I will not offer it.

Too simple console programs use simpler and simpler IDEs. But any IDE, ccp (even c ) programs can only be launched from the main one.

+1
source

Source: https://habr.com/ru/post/1492054/


All Articles