问题
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?
回答1:
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:
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:
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...
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. ;)
回答3:
Assuming you dropped several .cpp
files in the same project (containing a main in each), you can :
- Right-click on your
.cpp
file andProperties > 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...
回答4:
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.
回答5:
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.
回答6:
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.
回答7:
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.
来源:https://stackoverflow.com/questions/17164197/eclipse-creating-projects-every-time-to-run-a-single-file