I have NVIM v0.4.3 on macOS Mojave version 10.14.6 I have different version of g++ compilers on my machine and made the following alias in my .zshrc file:

alias g++="/usr/local/bin/g++-9"
alias g++Old="/usr/bin/g++"

I want to compile and run the following script on my Mac that I named count6.cpp:

#include <iostream>
#include <vector>

using namespace std;

int main()
    vector<int> vec;
    vec.push_back(8); // vec{4, 1 8}
    for(auto elem: vec)
        cout << elem << " ";
    return 0;

If I run the command the

g++ count6.cpp -o count6; ./count6

I get the following output:

11 1 8 %

However if I type the same command from within nvim, I get the following warnings:

:!g++ count6.cpp -o count6; ./count6
count6.cpp:12:9: warning: 'auto' type specifier is a C++11 extension [-Wc++11-ex
    for(auto elem: vec)
count6.cpp:12:18: warning: range-based for loop is a C++11 extension [-Wc++11-ex
    for(auto elem: vec)
2 warnings generated.
11 1 8

Ny interpretation is that the command from within nvim does not point to the same binary. How can I make point to the same binary?

  • It may be that aliases dont have effect when using :! (which wouldnt surprise me). Try :!type g++. Also make sure it isn’t a PATH issue. Lastly, use a makefile or other buildsystem that you can invoke with :make—then the problem disappears. – D. Ben Knoble Jun 20 '20 at 17:35
  • many thanks for the comment @D.BenKnoble. :!type g++ gives indeed g++ is /usr/bin/g++. Do you have other hint for implementing your 2 other advices? – ecjb Jun 20 '20 at 17:41
  • Be explicit and use g++-9. If you were using the native quickfix feature through :make (which appeared in the late 90's) instead of the old Vi (!= vim) :!compilername trick, you should be able to just set :let $CXX = 'g++-9' -- I expect your flavour of make is correctly configured with all the usual implicit rules. – Luc Hermitte Jun 20 '20 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.