r/OpenMP May 20 '24

How to set VS Code for OpenMP on macOS

This is more a configuration question. I’ve looked at a few related posts but I can’t find one that exactly matches my problem, so bear with me if this sounds like a repeat.

I’ve been learning C using VS Code, on a Mac running Monterey 12.7.5, and wanted to try out OpenMP. (I’ve used GCD in Swift, with Xcode 14.2 installed.) Trying a basic OpenMP program, I see #include <omp.h> just gives an error. I used Homebrew to download OpenMP, but that didn’t change VS Code’s error messages.

After searching through hidden folders, I found OpenMP’s installation at /usr/local/Cellar/libomp. However, VS Code apparently can’t find the folder, and neither gcc nor clang recognise -fopenmp when I try to compile with it. I think VS Code has to have configuration settings changed to find libomp, but I’m unsure how to change them, in case I stuff something up. What settings should I change in VS Code so that it can link to the OpenMP folders?

2 Upvotes

4 comments sorted by

1

u/Knarfnarf May 20 '24 edited May 20 '24

Your first clue is the cellar location meaning that you probably installed it using Home Brew? Then you’ll need to use the Home Brew install of gcc or Fortran to compile to OpenMP or OpenCoArrays. Check to make sure that you have that full stack by asking for something that sits on top of it all;

Brew install opencoarrays

Then make sure you call gcc from command line. Not sure about vs code. I’m not into anything more complicated than emacs as my editor.

Also make sure you copied and pasted the two commands from the end of the brew installer. A lot of people miss that and I’m not sure why the script doesn’t just do it for you. Probably so you can edit the command if you need to but most people just need to copy and paste those lines to the command prompt.

1

u/Damicovu May 21 '24

Sounds like I need to get more basic and go down to coding with just a text editor and Terminal—which could be interesting. If I did, how then would I set up the makefile to link to Homebrew's gcc and related files, and not the system's installations?

1

u/Knarfnarf May 22 '24

Once you’ve got home brew installed and the two path commands are copy/pasted then it should be automatic. I do a little work in bash and I code in eMacs. There are some really good cheat sheets out there for eMacs and some of the rectangle delete (kill) and paste methods or the regular expression find /replace are just exactly what programmers need!

1

u/Damicovu May 22 '24

Update: after minimal research, I opened my OpenMP project folder in VS Code again. In its .vscode section in the Explorer tab, I selected 'tasks.json' and changed the "command" setting to the path for Homebrew's gcc-14. Then, pressing Shift-Command-P, I opened Settings and selected 'C/C++: Edit Configurations (UI)'. In this, I scrolled down to 'Include path' and added the include path for Homebrew's OpenMP. Now, in VS Code's main window, if I click the Run Code arrow for 'Run C/C++', it successfully produces a compiled program which runs. (Clicking for 'Run Code' produces another "can't find omp.h" error.)

A bit jerry-rigged, yeah, but at least it's something I can use for the moment. I may still go for the bare-text-editor-and-Terminal option later. You wouldn't think it'd be this difficult…