Thread: want a .bat file to call the c compiler to compile and run c program

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    want a .bat file to call the c compiler to compile and run c program

    hi there ,

    i want to call the c compiler (using mingw) that can compile and run my c progarm through the .bat file
    can anyone can help ...
    can enyone give me a small example

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    in your .bat file you should just need 2 commands: first run the command to compile the program, then on the next line type the name of the file. (both assuming current directory, otherwise specify complete path, of course).

    thats the easy part. what you need to know is the command to compile your source code, which i imagine would be on the compiler's website or in its documentation.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Is there any particular reason that you want to do this?

    Anyway, assuming MinGW is in your path, something like this should work.
    Code:
    g++ program.cpp -o program.exe
    program.exe
    You might want to specify the paths to program.cpp or .exe, etc.

    You can also compile multiple files this way, by specifying more than one .cpp file -- or you can only compile the .cpp files that need compiling . . . but you quickly lose track of which ones need recompiling and which ones do not.

    If you start using multiple source files, use make. Even if you're only using one source file, a Makefile can make (sorry) your life a lot easier.

    Here's a very simple makefile to compile program.cpp into program.exe.
    Code:
    program.exe: program.o
    	g++ program.o -o program.exe
    
    program.o: program.cpp
    	g++ -c program.cpp
    Save that as "Makefile", type make, and bam, away you go. (Or call it whatever and use make -f whatever.)

    Note that you have to use tabs in Makefiles. Not spaces.

    If you want to learn more, find a make tutorial. There are lots out there.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what happens if the compilation fails?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    Well i was trying to do that but not able to what i need is to call the compiler through the bat file and pass my c program on command line and execute it ......well i know how to do it on command line its very simple
    its like
    c:\dev-cpp\bin gcc -o c:\project\embeddprogram c:\project\embeddprogram.c
    it orks fine through command line
    but when i am trying to create the batch file with this its not working ....
    so i want help to find out how can i make the .bat file that will call the gcc compiler and compile my c programm....

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Can you be more specific than "it doesn't work".
    Does it for example complain about not being able to find gcc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  3. OpenScript2.0 Compiler
    By jverkoey in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 01:52 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Trying to make rand different per program run
    By Dreamerv3 in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2002, 03:26 AM