How to compile and run C in sublime text 3?

前端 未结 12 1103
生来不讨喜
生来不讨喜 2020-11-30 20:33

I would like to compile and run C program in sublime text 3 on ubuntu 14.04. Currently the program is being compiled with gcc using sublime text 3 executing a command (see b

相关标签:
12条回答
  • 2020-11-30 21:11

    Have you tried just writing out the whole command in a single string?

    {
    "cmd" : ["gcc $file_name -o ${file_base_name} && ./${file_base_name}"],
    "selector" : "source.c",
    "shell": true,
    "working_dir" : "$file_path"
    }
    

    I believe (semi-speculation here), that ST3 takes the first argument as the "program" and passes the other strings in as "arguments". https://docs.python.org/2/library/subprocess.html#subprocess.Popen

    0 讨论(0)
  • 2020-11-30 21:13

    Are you using sublime text on linux? I got the same problem and it was solved! Here is my c.sublime-build:

    {
     "shell_cmd" : "gcc $file_name -o $file_base_name && ./$file_base_name", 
     "selector" : "source.c",
     "shell":true,
     "working_dir" : "$file_path"
    }
    
    0 讨论(0)
  • 2020-11-30 21:14

    The best way would be just to use a Makefile for your project and ST3 will automatically detect build system for your project. For example. If you press shift + ctrl/cmd +B you will see this: Make

    0 讨论(0)
  • 2020-11-30 21:17

    Instruction is base on the "icemelon" post. Link to the post:

    how-do-i-compile-and-run-a-c-program-in-sublime-text-2

    Use the link below to find out how to setup enviroment variable on your OS:

    c_environment_setup

    The instruction below was tested on the Windows 8.1 system and Sublime Text 3 - build 3065.

    1) Install MinGW. 2) Add path to the "MinGW\bin" in the "PATH environment variable".

    "System Properties -> Advanced -> Environment" variables and there update "PATH' variable.

    3) Then check your PATH environment variable by the command below in the "Command Prompt":

    echo %path%
    

    4) Add new Build System to the Sublime Text.

    My version of the code below ("C.sublime-build").

    link to the code:

    C.sublime-build

    // Put this file here:
    // "C:\Users\[User Name]\AppData\Roaming\Sublime Text 3\Packages\User"
    // Use "Ctrl+B" to Build and "Crtl+Shift+B" to Run the project.
    // OR use "Tools -> Build System -> New Build System..." and put the code there.
    
    {
        "cmd" : ["gcc", "$file_name", "-o", "${file_base_name}.exe"],
    
        // Doesn't work, sublime text 3, Windows 8.1    
        // "cmd" : ["gcc $file_name -o ${file_base_name}"],
    
        "selector" : "source.c",
        "shell": true,
        "working_dir" : "$file_path",
    
        // You could add path to your gcc compiler this and don't add path to your "PATH environment variable"
        // "path" : "C:\\MinGW\\bin"
    
        "variants" : [
    
            { "name": "Run",
              "cmd" : ["${file_base_name}.exe"]
            }
        ]
    }
    
    0 讨论(0)
  • 2020-11-30 21:18

    We can compile the code of C in Sublime Text and can print some value or strings but it does not accept input from the user. (Till I know... I am sure about compiling but not about output from given input.) If you are using Windows you have to set the environment variables for Sublime Text and GCC compiler.

    0 讨论(0)
  • 2020-11-30 21:18

    try to write a shell script named run.sh in your project foler

    #!/bin/bash
    ./YOUR_EXECUTIVE_FILE
    ...AND OTHER THING
    

    and make a Build System to compile and execute it:

    {
          "shell_cmd": "make all && ./run.sh"
    }
    

    don't forget $chmod +x run.sh

    do one thing and do it well:)

    0 讨论(0)
提交回复
热议问题