0

As the title suggests, I'd like to know how to add -pthread to the c/c++ compiler options of Sublime Text 3 on an Ubuntu system (gcc --std=gnu99 -pthread -Wall). I've searched online for an answer and couldn't identify any relevant solutions.

I'm still new at this and you would have my gratitude. Thank you.

UPDATE: Thank you for your responses. I've implemented @MattDMO's suggested solution and it seems like the compiler has stopped screaming for the most part, however, it's still not working with the below error message returned:

gcc: fatal error: no input files  
compilation terminated.  
[Finished in 0.0s with exit code 4]  
[cmd: ['gcc', '--std=gnu99', '-pthread', '-Wall', '/myworkingdirectory/myfile.c', '-o', 'myfile']]  
[dir: /myworkingdirectory]  
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]"

I'm not sure why it's complaining of no input files. Any thoughts?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
kfkhalili
  • 996
  • 1
  • 11
  • 24
  • Have you read the [documentation for build systems](http://docs.sublimetext.info/en/latest/file_processing/build_systems.html)? It should be pretty clear how to add arguments. – Some programmer dude Jun 26 '14 at 11:40
  • I think your problem, is expecting a programming editor to be a fully featured IDE, if you want an IDE that runs on Ubuntu & Windows, CODE::BLOCKS works fine and is not only free but FOSS. – Rob11311 Jun 26 '14 at 12:13
  • @Rob11311 Sublime builds C/C++ programs just fine, you don't need an IDE for that. – MattDMo Jun 26 '14 at 15:16
  • 1
    @Rob11311, thank you for your suggestion. The thing is, I'm only starting out and have tried Eclipse, CODE::BLOCKS, and other IDEs. However, I've noticed that the learning curve is quite steep and will put in the time to choose an IDE during my time off. For the time being, Sublime Text is easy to install and code in, and until now, it's been performing well. I will definitely keep your advice in mind. – kfkhalili Jun 26 '14 at 16:54
  • @kfkhalili Have you tried running `gcc --std=gnu99 -pthread -Wall myfile.c -o myfile` from the command line? Make sure you're in `/mydirectory` while running it. Also, did you save `myfile.c` before building? – MattDMo Jun 26 '14 at 17:30
  • Your compile command is wrong for sure, I've given more info in my answer, so you can see how SIMPLE this OUGHT to be – Rob11311 Jun 26 '14 at 19:12
  • Thank you for your assistance. I had copied the JSON code incorrectly. Things are fine now. @Rob11311, thank you for your suggestions. I will definitely look into the use of Makefiles and IDEs. – kfkhalili Jun 27 '14 at 08:54

2 Answers2

0

To add compiler options, you'll need to to either modify Sublime's standard C/C++ build system, or create a new one. The easiest method is to create a new one, but I'll also show you how to edit the existing file.

To create a new build system, open a new file in Sublime with JSON syntax and add the following:

{
    "cmd": ["gcc", "--std=gnu99", "-pthread", "-Wall", "${file}", "-o", "${file_base_name}"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",
    "shell": true,

    "variants":
    [
        {
            "name": "Run",
            "cmd": ["${file_path}/${file_base_name}"],
            "shell": true
        }
    ]
}

Save it as ~/.config/sublime-text-3/Packages/User/C_pthread.sublime-build. To compile with it, open your source file, then select Tools -> Build System -> C_pthread, then hit CtrlB to build. CtrlShiftB will execute the Run option.

Editing the Sublime's built-in C/C++ build system is slightly more involved. In Sublime Text 3, packages are wrapped up in .sublime-package zip archives. To access the C/C++ build file, first install Package Control (if you haven't already), then install the PackageResourceViewer plugin. Open the Command Palette with CtrlShiftP, type prv to bring up the PackageResourceViewer options, select Open Resource, then navigate down to C++ and select the C++.sublime-build option. Replace its contents with those above, then save the file. The advantage of this method is that you can select Tools -> Build System -> Automatic, and whenever you're editing a C file you can hit CtrlB to build and not worry if the correct build system is selected.

One final bit of advice: editing build systems is fine for small, one-off programs, but for larger projects I'd strongly suggest using make. You can set all your options in your Makefile, open it in Sublime, hit CtrlB (assuming your Build System is set to Automatic), and your program will build. This way you don't need to mess around with setting all the command-line options, library includes, etc. in the .sublime-build file, then change them around again for your next project.

Good luck!

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Doing this is Sublime build defintion is ugly, complicated and ignores the much simpler and better supported Linux/Ubuntu tools like make, cmake etc – Rob11311 Jun 26 '14 at 15:36
  • @Rob11311 - which is why I put the note at the end to consider using make, which is also accessible through Sublime. This answer addresses the OP's question, which is how to add compiler options to a Sublime Text build system. Don't downvote an answer because you disagree with the question. – MattDMo Jun 26 '14 at 15:49
  • The key is the OP saying "I'm still new to this", a declaritive Makefile solution is so much simpler and superior, understood widely by the Ubuntu community, providing JSON like that is considered harmful. – Rob11311 Jun 26 '14 at 16:13
  • @Rob11311 if that's the case, then create a useful answer with an example Makefile for the OP to use, along with clear instructions on how to utilize it. The reason your answer was downvoted was because it really wasn't an answer, just throwing around a bunch of terms that the OP, being new, probably doesn't fully understand, along with a link to an irrelevant forum post where the problem is with C++11 and Postgres headers. OTOH, my answer offers a clear set of instructions, solves the OP's question, **and** offers advice of what to do in the future regarding Makefiles. – MattDMo Jun 26 '14 at 17:49
  • The link I included and thread is quite sufficient to show the horror.. and a more appropriate place to get help on it, where you're more likely to get specifics. Most questions like this get stomped on by SO'ers who love to moderate heavily and lock/hold, so spending a lot of time on these threads tends to be pointless. Finding GNU make examples and tutorial with a search engine is very simple. If the OP had got back and asked for an exmple Makefile, I would have done it. – Rob11311 Jun 26 '14 at 18:55
-1

This is really just about setting compile & link flags to gcc/g++. Found something about adding options to sublime's build system on their forum, which suggests you are better not to do that.

Get SublimeText 3 to comile C++11 (sic)

Really on Ubuntu, I would learn to compile & build with GNU make, or just simply in your shell set the LDFLAGS environment variable (in bash export LDFLAGS=-lm or export CCFLAGS=-Wall for example) and hope that convention is respected by sublime text 3. If it doesn't understand that, then it's not following Ubuntu POSIX programming environment conventions and that is likelky to become more and more of a issue with time.

I am suspicious about the provided command line flags in question -pthread, I would expect the option to be -lpthread that is "ell for library". A tutorial showing linking with POSIX thread library is found in this link : Pthread tutorial including C/C++ compile & link commands

Make tutorial GNU Make in detail for beginners You can set CFLAGS & LDFLAGS as explained in this SO answer How to use LDFLAGS in makefile

Community
  • 1
  • 1
Rob11311
  • 1,396
  • 8
  • 10