12

i am a beginner using eclipse CDT. usually in DEVc++ or other lightweight IDE's we can directly open and edit a single .cpp file from desktop and run it.

on the other hand i cannot find this simple feature in eclipse CDT! everytime i need to run a single .cpp file i need to create a project!

for competetive programming such as in TOPCODER copetetions or CodeForces i usually want to run single files quickly!

is there a way to run single .cpp files in Eclipse CDT without needing to create a project everytime?

Shivendra
  • 1,542
  • 2
  • 22
  • 35
  • You could have one project pre-created and just put the file into it, I assume – Angew is no longer proud of SO Jun 18 '13 at 08:40
  • 1
    For testing and throw away code, I have one project pre-created and then I use this one for it. – Devolus Jun 18 '13 at 08:41
  • @Devolus can't u directly run a single cpp file? – Shivendra Jun 18 '13 at 14:48
  • @Angew evrytime i want to test a cpp...i will need to create projects.That is my problem! – Shivendra Jun 18 '13 at 14:49
  • 1
    in lightweight IDE's like famous geany,Dev C++ etc. A simple Ctrl+N and a new file is created...which can run! can't we do this in eclipse CDT? – Shivendra Jun 18 '13 at 14:54
  • You can have one project always present, and change the file in it to whatever you're currently testing. – Angew is no longer proud of SO Jun 18 '13 at 14:59
  • @Angew means you finally say that we cannot run a single cpp file in such a good IDE like eclipse? the empty project create and test is just not what i am looking for.. – Shivendra Jun 18 '13 at 15:02
  • @Angew if u have run Dev C++ we can directly double click on a cpp file on desktop,edit and run it ! can't we configure eclipse in doing this? – Shivendra Jun 18 '13 at 15:03
  • Single-file applications are not the norm. There's little worth in optimizing for that case. – nitind Jun 18 '13 at 15:30
  • 1
    @nitind in competetive programming sites like topcoder or codeforces we are required to submit single running files not applications! single running files are a requirement given the number of users coding in topcoder type sites!! – Shivendra Jun 18 '13 at 15:38
  • 1
    I've had enough of this crap. :) I'm a beginner with Eclipse too. So after messing around with the solutions on this page for an hr **I finally just opened up the single file in the *Code::Blocks IDE* instead of Eclipse, then I clicked the "Build and run" button and voila!**--a few seconds later the application magically ran and worked!--even though it was in a Windows folder with 5 other files--all stand-alone C++ programs with their own main() functions. – Gabriel Staples Aug 05 '17 at 14:23
  • @GabrielStaples but CodeBlocks complained about multiple main for me ! Can you describe how you did it ? – Aritro Shome Oct 21 '21 at 12:48
  • @AritroShome, it's been 4 yrs; I don't remember. However, I'll tell you this: today I don't use Windows, I use Linux Ubuntu and build and run my code with this command, for example: `g++ -Wall -Wextra -Werror -std=c++17 -o hello_world hello_world.cpp && ./hello_world`. That builds and runs `hello_world.cpp`. It is possible to run this command in Windows too. I recommend [installing the WSL (Windows Subsystem for Linux)](https://docs.microsoft.com/en-us/windows/wsl/install), installing `g++` inside the Ubuntu WSL ([see here](https://askubuntu.com/a/398491/327339)), and then using my cmd above. – Gabriel Staples Oct 21 '21 at 21:24
  • @AritroShome, for more build commands and examples, see my [eRCaGuy_hello_world](https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world) repo and look at the various example files. Each file contains build instructions and information in the comments at the top of the file. – Gabriel Staples Oct 21 '21 at 21:26

8 Answers8

6

Eclipse is great. I love it, but you are right, there is too much burden with one-file projects. So maybe approach problem in different way:

  1. Write code in Eclipse enjoying all IDE features.

    Put all your one-file programs to single project. No, Eclipse won't compile them because of multiple main functions but...

  2. Compile file from command line.

    You can copy proper command line from Eclipse console when building project. You must be smart because you are playing topcoder so you figure out how to alter command line for your need. ;)

omikron
  • 2,745
  • 1
  • 25
  • 34
4

Assuming you dropped several .cpp files in the same project (containing a main in each), you can :

  • Right-click on your .cpp file and Properties > C/C++ build > Settings > Manage Configurations.
  • Create a new configuration and set it as the active one + OK.
  • Select the newly created configuration.
  • Select the files you don't want to run for this configuration, open Properties and check "exclude resource form build", for each one.
  • Select the file(s) you want to keep for that configuration and make sure "exclude resource form build" is unchecked.
  • Build the Project
  • Press button play

Finally, to switch from one run configuration to another, go to Project > Properties > Manage Configurations and change the active one. Rebuild and run.

EDIT: as shivi mentioned, the most painful part (excluding resources form build) can be done in one single shot by multi-selecting the files to exclude in the Package Explorer view and Right-click > Resource Configurations > Exclude from build....

That's the best we can do...

Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85
  • 2
    exclude from build option can directly be done from project explorer. Right click on file option is present. But doing this for every .cpp file is too much to do for just 1 file! – Shivendra Jun 21 '13 at 15:20
  • Ok, thanks for the useful remark. And sorry that I couldn't provide more than what you already knew... – Gauthier Boaglio Jun 21 '13 at 15:31
4

Create a new Folder (ex: tmplib)

Right click on "tmplib" -> Resource Configuration -> Exclude from build (debug and release)

Drag/drop files between src and "tmplib" folder - anything in src folder will be built (no need to delete src folder)

2

That's what I do. You can create a project called Coeforces for example, then you can create a folder inside the project called AC for example and right click on it --> Resource Configuration --> Exclude from build --> check both Debug and Release .

Then delete src folder. Whenever you finish a code just drag and drop it in the folder and then you can create a new source file directly in the project . Also you can create another folder called WA for example for problems that you couldn't solve and want to save the code in order to think again later.

2

I know eclipse has the ability to create a java scrap book, using this you can write a bit of java code and just run that code, with out having to setup any classes or main functions. You might want to look to see if eclipse has C++ scrap book in the New dialog box.

mpop
  • 499
  • 11
  • 21
0

Use geany. It works out of the box with single C++ file. And it can run a custom make file if you need to modify your setting.

0

Create a Makefile project File->New->C++ Project->Makefile project->Empty project->Linux GCC.

Then create a Makefile for your project and add the following lines:

CXXFLAGS := -std=c++14 -Wall -Wextra -pedantic-errors    

SOURCES  := $(wildcard *.cpp)
PROGRAMS := $(patsubst %.cpp,%,$(SOURCES))

all: $(PROGRAMS)

clean:
    $(RM) $(PROGRAMS)

Note: the indentation of the command after clean: is using a TAB (spaces will not work).

That will compile any source file in the project directory that ends in .cpp.

Then select Outline View window. Select and add all and clean as targets. Then open the Build Targets window to compile or clean your project.

Galik
  • 47,303
  • 4
  • 80
  • 117
0

In Visual Studio Code there is a Code Runner plugin to build and run single files, but in eclipse I couldn't find one.

I have tried out an alternative way, using the "External Tools" feature:

  • Create a project and put all your code in it.

  • At somewhere create a script file run.bat on Windows or run.sh on Linux, something like this:

g++ %1 -o %1.exe
%1.exe

...where %1 represents the 1st argument passed to the .bat and should be replaced by $1 in .sh.

  • Find the "External Tools Configurations" on the toolbar...

image

...or in the "Run" menu.

  • Add a "Program" configuration like this:

image

"Location" is the path to the run script you just created;

"Arguments" will be passed to your script. The ${resource_loc} represents the file you are currently editing.

  • Save the configuration and close the window.

Afterwards you can open a cpp file and run this tool. Your program will launch in the console.

image

e792a8
  • 1
  • 2