0

I wrote some C++ code in Windows. When I run make fileName from the Visual Studio Code terminal I see the command that make runs is g++ fileName.cpp -o fileName.

I then compile the same file in macOS Catalina using the same command make fileName and the command that make runs is c++ fileName.cpp -o fileName. I have run make -p -f /dev/null and saw that the CXX variable is set to c++ which is an alias to clang.

I would like make to use g++ on macOS as it does on windows.

I have seen posts mentioning you can change the implicit variables but haven't figured out how to do it. I already changed the default compiler in VS Code from usr/bin/clang to usr/bin/gcc and also tried with usr/bin/g++ but the command doesn't change to g++ fileName.cpp -o fileName as desired.

I have also tried running CXX=g++, and CXX='/usr/bin/g++ in the terminal but that didn't work.

Is there a way to do it? I would like to not have to create a makefile in my directory and instead just change the implicit rule.

d_14
  • 1
  • 1
    `make` is designed to read a makefile. The implicit rules aren't meant to replace a makefile, they are meant to simplify it. If you replace the `make` installed on your system with one using different implicit rules, you will break the build for every other piece of software that uses `make`. – Ben Voigt Jan 31 '22 at 17:12
  • Have you tried `CXX=g++` in the makefile? – littleadv Jan 31 '22 at 17:13
  • You have to read the `make` manual pages to find the default rules. But you should note that all the rules are defined in terms of variables (that have default values) that can be overridden in the shell. Have a look at your environment variables on both machines and see if you can spot any obvious ones. Note: `CXX` specifies the C++ compiler. – Martin York Jan 31 '22 at 17:38
  • For C++ programs the formula are: `$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c .cpp` and then to link `$(CXX) $(LDFLAGS) .o $(LOADLIBES) $(LDLIBS)` – Martin York Jan 31 '22 at 17:43
  • These links may help: https://stackoverflow.com/q/15745241/14065 and https://www.gnu.org/software/make/manual/html_node/Implicit-Rules.html – Martin York Jan 31 '22 at 17:45
  • @MartinYork Would you mind pointing me in the right direction to override the implicit variables using the shell instead of a makefile? None of my environment variables seem to be altering any of the make variables. I'm also unsure how to create an environment variable that will successfully override the CXX variable in make. How will make read my environment variable? And what should that environment variable look like? – d_14 Jan 31 '22 at 19:07
  • @BenVoigt does that mean that there is no way to set the CXX implicit variable to g++ other than creating a makefile in the directory that my file is in? Or is it just not advised? I'm a beginner and don't really need to compile anything that would require any complex rules so creating a makefile is an extra step that I would like to avoid at this time. – d_14 Jan 31 '22 at 19:10
  • It's not advised to try to configure `make` without using the file made for storing `make` configuration (`Makefile`). You can do `make CXX=g++ filetomake`. If you don't want to type `CXX=g++` it every time you run make, create a `Makefile` that contains nothing but `CXX=g++`. You can still list targets on the command-line (`make filetomake`) when you have a Makefile. – Ben Voigt Jan 31 '22 at 20:12
  • @BenVoigt Understood. Thank you for your help. – d_14 Jan 31 '22 at 22:07
  • The general form of a `make` invocation is `make GOAL... VAR=VALUE...`. So, simply override the `CXX` make variable on the command line: `make CXX=g++`. But if you really want to use `g++` under macOS first check that it is `g++`. Apple prefer `clang` (probably for licenses reasons) and they tend to redirect to it, even if you call `g++`. Try `g++ --version` and carefully read the output. If it is indeed `clang`, try maybe to install the real `g++` with macports or Homebrew. – Renaud Pacalet Feb 01 '22 at 14:07

0 Answers0