Thread: Compile C file from command line - to a different path.

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Compile C file from command line - to a different path.

    Hi,

    I'm working on windows and I'd like to know how to compile the C file to a different path.

    What I mean is :
    the basic compile command is :gcc Hello.c -o Hello_E

    I'd like to create the "hello_E" in a different path.
    Something like this:
    gcc Hello.c -o C:\Program Files\PellesC\C_programs\Exe_files\Hello_E

    thanks in advanced

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Use a makefile

    Normally, you have two steps

    make build
    to compile the source code, and produce an executable in the build directory.

    make install
    to copy the executable to it's final place (along with anything else that needs doing).

    Presumably, you tried
    gcc Hello.c -o C:\Program Files\PellesC\C_programs\Exe_files\Hello_E
    and it didn't work, in which case you should post your error messages.

    Or maybe you haven't actually tried it yet....
    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.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by ilans11il View Post
    I'd like to create the "hello_E" in a different path.
    Something like this:
    gcc Hello.c -o C:\Program Files\PellesC\C_programs\Exe_files\Hello_E
    Try turning the backslashes into forward slashes. gcc was originally designed as a compiler for unix-type systems, which separate path elements with forward slashes rather than backslashes. Windows (or, more accurately, the win32 API) treats backslashes as (largely) requirement for forward slashes in paths anyway, even though programs targeting windows rarely use them overtly.

    If you're providing that string in a C program (for example, calling system("gcc Hello.c -o C:\Program Files\PellesC\C_programs\Exe_files\Hello_E")) bear in mind that backslash is an escape character in string literals. The solution there is either to (again) turn the backslashes into forward slashes, or to prefix the backslashes with another backslash (for example, system("gcc Hello.c -o C:/Program Files/PellesC/C_programs/Exe_files/Hello_E"))

    Either way, gcc does not create directories for output files to go into. So if the directory C:\Program Files\PellesC\C_programs\Exe_files does not exist, creation of a file named C:\Program Files\PellesC\C_programs\Exe_files\Hello_E will fail.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line compile include libraries
    By Ducky in forum C++ Programming
    Replies: 2
    Last Post: 11-29-2011, 11:44 AM
  2. How to configure MSVC# for command line compile?
    By thetinman in forum C# Programming
    Replies: 2
    Last Post: 12-02-2008, 11:11 AM
  3. how to compile on the command line
    By majoub in forum C Programming
    Replies: 2
    Last Post: 04-23-2005, 07:40 AM
  4. Command Line path
    By snakattak in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-27-2002, 10:31 PM
  5. Replies: 4
    Last Post: 11-14-2001, 10:28 AM