how to test C or C++ snippet quickly?
Asked Answered
E

5

9

I am using Ubuntu and Eclipse as an IDE for C/C++.

I currently have a big project in Eclipse. Sometimes, I want to test some small functions written in C/C++ but I don't want to re-create a new project in Eclipse. It is much time consuming and slow. I want to ask if there is any better way to do this ?

(In the past, I usually used a combination of GEDIT and GCC from the shell, but I really like the auto-completion or intellisense feature in Eclipse, which GEDIT does not have. I have also tried Scribes but it does not have a full intellisense feature like Eclipse)

Estafette answered 25/7, 2011 at 11:30 Comment(3)
What's wrong with just having a vanilla sandbox project in parallel? I often have something with only one main.cpp opened next to my main project.Retsina
it is not convenient to switch among projects and projects in Eclipse, especially when a project gets large in number of files, etc. That is why I want to use a different tool for this.Estafette
@tsubasa: Why not have two separate instances of Eclipse running?Metaphosphate
F
13

Use online compiler like Ideone or Codepad.
Ofcourse, they dont provide you auto code completion feature & other fancy features but that is the price you pay for quick & easy way of checking stand alone functions.

Fermata answered 25/7, 2011 at 11:32 Comment(4)
Wow, someone even managed to find a reason for Downvoting this! :) I wonder what it may be?Fermata
@tsubasa: Did you even read the answer? I explicitly mentioned it wont autocomplete & even the reason for it.Fermata
yes, my comment is an agreement to yours so i did read the answer.Estafette
I would be interested in the reason for down-voting this, too. Occasionally, I (and we all, I think) get downvoted for no obvious reason.Armelda
A
7

This method works without an internet connection and without exposing your code.

<ctrl>+<alt>+T                        <-- 0) opens a terminal

vi test.cc                            <-- 1) hackery
...
g++ -Wall -Wextra test.cc && ./a.out  <-- 2) compile + run
rm test.cc                            <-- 3) clean up (optional)

Replace vi with your favourite editor or cat. Can't be less obtrusive.

Some editors like SciTE have some very basic code completion (btw btw: SciTE has shortcuts to directly compile and run code from within the editor).

Btw: QtCreator gives some decent "intellisense", and the project files are minimal. A single project file line is enough for such one-function-test.


unkulunkulu points out that you can also replace step 2 like this (there should better be no Makefile in your try-out folder; could conflict with existing targets in that):

<ctrl>+<alt>+T                  <-- 0) opens a terminal

vi test.cc                      <-- 1) hackery
...
make test && test               <-- 2) compile + run
rm test.cc                      <-- 3) clean up (optional)

It has the tiny disadvantage that telling g++ about extra arguments (like -Wall or -std=c++0x is a bit more obtrusive).

Armelda answered 25/7, 2011 at 11:34 Comment(4)
I use make test instead of g++ test.ccHammack
@unkulunkulu: test.cc was just an exemplary name for some functions where you want to test a small code-snippet. It could have been equally validly foo.cc or sdfkljsdlfkjsdklfj.cc. My answer was not related to unit testing ;)Armelda
in provided cases I would use make foo or make sdfkljsdlfkjsdklfj. It works without a makefile thanks to default implicit rules.Hammack
@unkulunkulu: I see, I wasn't aware of that. Pretty cool. My false observation was then based on the fact that many projects use make test to run their automatic test-procedures, and I thought you would be referring to that. +1 :)Armelda
C
1

I will advise you to use gedit with the embeded terminal plugin.It allows quick compiling through the embeded terminal.Perfect for quick testing.

Candlewick answered 25/7, 2011 at 11:37 Comment(0)
M
1

You can use tcc as a C script engine.

$ cat tcctest.c
#!/usr/bin/tcc -run
#include <stdio.h>
int main(void) {
    printf("Hello, tcc!\n");
    return 0;
}
$ chmod u+x tcctest.c
$ ./tcctest.c
Hello, tcc!
Mcqueen answered 25/7, 2011 at 11:41 Comment(1)
@phresnel: if tsubasa is writing multi-language source files, testing small functions is, I guess, the least of his worries.Mcqueen
L
0

http://www.compileonline.com I found this Site more useful than ideone or codepad because it supports more languages than codepad and you can see the output of you code on an adjacent window you can also provide Standard Inputs and command line arguments and you can also access a file input.txt in your program.

CompileOnlineScreenShot

Ladle answered 3/2, 2014 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.