Compiling from the command line

This is a discussion on Compiling from the command line within the C++ Programming forums, part of the General Programming Boards category; I want my program to compile another .cpp file, but I'm having some trouble: I've searched this board and googled ...

  1. #1
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300

    Compiling from the command line

    I want my program to compile another .cpp file, but I'm having some trouble:

    I've searched this board and googled it and came up with the gcc command.

    Supposedly, since Dev-C++ uses the gcc compiler (right?) I should be able to compile with:

    Code:
    gcc myfile.cpp -o myfile
    or possibly

    Code:
    gcc -o myfile.cpp myfile
    however, when I include the command in my program:

    Code:
    system("gcc -o myfile.cpp myfile");
    it doesn't work, saying that gcc is "not recognized as an internal or external command, operable program, or batch file."

    Should I be trying to use a different directory? Using something other than the system() command?

    My other problem is with the system command specifically. I don't know how to get it to accept spaces in the directories. For instance:

    Code:
    system("c:\\C++ projects\\myfile")
    will not execute myfile.exe, it will tell me that "c:\C++ is not recognized as a..." blah blah blah

    I tried using the escape character \x20 to add the space, but to no avail. Any ideas there?

    Sorry for putting two questions in one post, I probably should have separated them, but I got on a roll typing and here I am.

    Thanks in advance for your time.


    Decrypt

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Heres a hint: Give the full path to gcc

  3. #3
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I've tried:

    gcc -o c:\path\myfile.cpp myfile
    c:\path\gcc -o myfile.cpp myfile
    c:\path\gcc -o c:\path\myfile.cpp myfile

    into the command prompt directly, and:

    system("c:\\path\\gcc myfile.cpp -o myfile");
    system("gcc -o c:\\path\\myfile.cpp myfile");
    system("c:\\path\\gcc -o c:\\path\\myfile.cpp myfile");

    within the program.

    I get the same message each time.

    Dev C++ is stored and runs from c:\dev-c++\
    I assume this is were gcc runs from as well. It is also where it stores data files if I do not specify a path. Should I be using a different directory?
    Last edited by Decrypt; 09-13-2005 at 10:55 PM. Reason: forgot to specify

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I just found gcc. It's in the dev-c++\bin directory.

    Should have searched the computer for "gcc" before posting, sorry to waste your time.

    Now all I have to do is get it to work right....


    Decrypt

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    yes, look for the bin directory.
    or you can open Dev-C++ go to Tools -> compiler options -> directories -> binaries
    and see the directory g++ is stored in

  6. #6
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Now that I've found gcc (and g++, which I read online can be better for c++, is that true?) I can't get it to compile.

    I use the command:

    Code:
    system("c:\\dev-c++\\bin\\g++ -o c:\\dev-c++\\myfile c:\\dev-c++\\myfile.cpp");
    I get the error "g++: installation problem, cannot exec 'cpp': No such file or directory"
    (Same error if I use gcc instead)

    myfile.cpp is in c:\dev-c++\, and when I load the file into dev-c++ and compile it myself it works fine. I've also tried the command without the paths in the arguments, but got the same error. I've searched online, and everywhere the syntax for gcc/g++ matches what I'm using.

    Any ideas on what I'm doing wrong?

    Thanks again,

    Decrypt


    p.s. There's still the question of spaces in the system command from my first post, if anyone knows...
    Last edited by Decrypt; 09-13-2005 at 11:44 PM. Reason: code tags didn't work

  7. #7
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    because devc++ is using mingw to run gcc.

    you literally have to use the mingw command prompt to run gcc.
    it doesn't work with dos.

    there is a compiler that uses some of gcc "features" [ read as "bugs" more accurately ]
    it's here
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    and when I load the file into dev-c++ and compile it myself it works fine. I've also tried the command without the paths in the arguments, but got the same error.
    Have you tried using it from the command line first? Are you sure that the program in myfile.cpp compiles correctly? Also, it might be a good idea to post the smallest and simplest example program that demonstrates this problem.

    you literally have to use the mingw command prompt to run gcc.
    The 'mingw command prompt' that you are referring to is probably MSYS, but MSYS is not required to run the compiler. More likely one would use MSYS to run a configure script.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I'm sure that myfile.cpp compiles correctly - I made it very simple since I was just experimenting, trying to get the other program to compile it. I've tried compiling from the command line myself, but get the same message as when the program does it. It compiles and runs without error if I compile it using Dev-C++.


    Here's a simple example of the files in question:

    myfile.cpp:
    Code:
    //myfile.cpp - test program to be compiled by testprog.exe
    
    #include<iostream.h>
    #include<stdlib.h>
    
    int main()
    {
    
    cout << "Why has my compiler forsaken me?"; system("pause"); return 0;
    }
    testprog.exe:
    Code:
    //testprog.cpp - test program to compile myfile.cpp
    
    #include<stdlib.h>
    #include<iostream.h>
    
    int main()
    {
    
    system("c:\\dev-c++\\bin\\gcc -o myfile myfile.cpp"); **see note below system("pause"); return 0;
    }

    **I get the same error (see previous post) when I include the full paths (c:\\dev-c++\\myfile and c:\\dev-c++\\myfile.cpp) in the system() command.

    testprog compiles, but when it runs, I get the 'gcc: installation...' error, as if I'm not using gcc (or g++ if I try that) correctly.

    Decrypt

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You have to CD to c:\\dev-c++\\bin\\ first:

    Code:
    system("cd c:\\dev-c++\\bin");
    system("gcc -o c:\\dev-c++\\myfile c:\\dev-c++\\myfile.cpp");
    I know . . . I just did this myself.
    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.

  11. #11
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    This is the code exactly as I have it in my program:

    Code:
    void compile_program(char what[25])
    {
         system("cd c:\\dev-c++\\bin");
         system("gcc -o c:\\dev-c++\\myfile c:\\dev-c++\\myfile.cpp");
    
    }//end compile_program
    I get the error :" 'gcc' is not recognized as an external... ," the same as before.

    I have found that if I go the command prompt myself, cd to c:\dev-c++\bin, and use the command

    gcc -o c:\dev-c++\myfile c:\dev-c++\myfile.cpp

    it accepts the command, and attempts to compile the program, which brings up a different issue entirely and begs the question: Now I'm having strange trouble compiling from the command prompt with a .cpp file that compiles/runs correctly within Dev-C++. Should I start a new thread, or post the problem here as well? (I realize this is probably just a matter of etiquette, but as you can see I'm very new here - I'd like to know the proper way of doing things.)

    Anyway, though I can use the above gcc syntax from the command prompt, if I use the code listed above, which mimics it exactly (as far as I can tell), it doesn't work. Is the directory change somehow not carrying over to the next system() command?

    Sorry to beat a dead horse here, but what am I getting wrong?

    Decrypt

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    system("cd c:\\dev-c++\\bin");
    system("gcc -o c:\\dev-c++\\myfile c:\\dev-c++\\myfile.cpp");

    This doesn't work
    Each system() is a separate process, so the "cd" in the first call is lost when that process exists, and you're back where you started when the 2nd call to system is made.

    You really need to update the PATH to include c:\dev-c++\bin
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I tried:

    system("c:\\dev-c++\\bin\\gcc -o c:\\dev-c++\\myfile c:\\dev-c++\\myfile.cpp");

    but that didn't work, either. When I get home, I'll try it again, I could've mistyped something.

    Last night, I tried going to the command prompt myself (to see what works so I can put it into system() ) and from the c:\dev-c++ directory, typed:

    c:\dev-c++\bin\gcc -o c:\dev-c++\myfile c:\myfile.cpp

    It didn't recognize gcc as a command. However, if I cd to c:\dev-c++\bin and type:

    gcc -o c:\dev-c++\myfile c:\dev-c++\myfile.cpp

    It begins to compile. For some reason, it doesn't seem to work outside of c:\dev-c++\bin, which makes no sense to me. Can I put two commands into system()? (I thought of this while I was typing, so sorry if it's easy to research.)

    Decrypt

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    For some reason, it doesn't seem to work outside of c:\dev-c++\bin, which makes no sense to me.
    c:\dev-c++\bin is not in your system path. You might want to consider adding it to your system path.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I'm not 100% sure but try this:
    Code:
    system("c:\\dev-c++\\bin\\gcc.exe -o c:\\dev-c++\\myfile.exe c:\\dev-c++\\myfile.cpp");

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling Issues
    By pc_doctor in forum C Programming
    Replies: 3
    Last Post: 11-30-2007, 09:00 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Problem Compiling
    By Flakster in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 01:09 AM
  4. Compiling
    By Dae in forum C++ Programming
    Replies: 7
    Last Post: 06-15-2005, 01:08 AM
  5. compiling and executing issue with lcc win32
    By GanglyLamb in forum C Programming
    Replies: 10
    Last Post: 12-22-2004, 01:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21