0

I'm learning opengl these days and I'm compiling from command line (I don't want to use IDE because I'm used to VIM). Compiling the opengl files can be troublesome, say if I have a file called hello.cpp, for quickly compile&run I have to do:

g++ -framework GLUT -framework OpenGL -framework Cocoa hello.cpp & ./a.out

Since I'm learning opengl I'll be testing with lots of different files and it's not feasible for me to type the above command all the time. What I want is something like:

some_command % & ./a.out

The some_command is best to be some commonly used ones (like g++, make), and % is the filename.

I think makefile might be a possible solution, so I went ahead and learnt about makefile but it seems to be working for a specific name; there're methods to pass parameters to makefile but still it seems too troublesome for me (I have to manually type-in the ARGUMENT=filename part).

Also I think shell script might be a good solution.. the only fallback is that I have to self-define a name for the shell-script.

I want to know if there is any better way for me to quickly compile&run the opengl cpp files

Thanks a lot.

Community
  • 1
  • 1
songyy
  • 4,323
  • 6
  • 41
  • 63
  • What's is the problem with using a shell script? I don't understand what you mean by "I have to self-define a name for the shell-script." – Matt Ball Mar 27 '13 at 04:08
  • @MattBall If possible.. I'd like to use some commonly-used command instead of self-invent ones... say like I write a script named "compile-cpp-gl.sh", I'll prefer using make if I can get it done elegantly – songyy Mar 27 '13 at 04:31

2 Answers2

4

Use a Makefile

CC=g++
PARAM=-framework GLUT    \
      -framework OpenGL  \
      -framework Cocoa  #\

hello: hello.cpp
       $(CC) $(PARAM) ${ARGS} hello.cpp -o hello

Then compile and run with make hello; ./hello;

You might be wondering what the ${ARGS}. It's for custom command-line arguments. So now you can do something like make ARGS="-framework CustomX" hello; ./hello; and it will get added to the other g++ options you have in the Makefile.

You can read more about GNU Makefile here, here and here

UPDATE: Here is a small one-liner that will generate all the targets you need. If you want, you can expand it into a script so you have more control over it.

ls *.cpp | perl -ne 'chomp; $n=$_; $n=~s/\.cpp$//; print "$n: $_\n\t\$(CC) \$(PARAM) \${ARGS} $_ -o $n\n";' >> Makefile

Apart from that, I usually write my targets by hand. @Emil Sit's solution might be better if you want something really general.

wsdookadr
  • 2,584
  • 1
  • 21
  • 44
  • Sorry I think my question is not clear... I want to have some like "make FOO.cpp" to be able to compile the opengl cpp file called "FOO.cpp", without writing the same makefile every time. (because I want to quickly test different cpp files) – songyy Mar 27 '13 at 04:33
2

I think you want to be able to do something like:

make foo

(say, from vim), where foo varies depending on what source file you are using.

To achieve this in make, you want to use a pattern rule.

For example:

CXX=g++
CXX_FLAGS=-framework GLUT -framework OpenGL -framework Cocoa

%: %.cpp
    $(CXX) $(CXX_FLAGS) -o $@ $<
    ./$@

This should allow you to run make hello and it will:

  • Build an executable called hello in the current directory (if hello.cpp has changed).
  • Run hello for you, if it decided to compile and the compilation was successful.
Emil Sit
  • 22,894
  • 7
  • 53
  • 75