Can't compile C++ 'hello world' with `#include` after upgrading to Mojave

别来无恙 提交于 2019-12-11 08:47:54

问题


CS student, compiling worked fine before upgrading to Mojave. After doing so I cannot get anything to work if it has an include statment. For example:

#include <iostream>
int main()
{
    std::cout << "length:" << 10 << std::endl;

    return 0;
}

On compiling:

$ g++ -ferror-limit=3 main2.cpp -o test
In file included from main2.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:477:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string_view:176:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__string:56:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:642:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:203:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstdint:158:8: error: no member named 'uint8_t' in the global
      namespace
using::uint8_t;
     ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstdint:159:8: error: no member named 'uint16_t' in the global
      namespace
using::uint16_t;
     ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstdint:160:8: error: no member named 'uint32_t' in the global
      namespace
using::uint32_t;
     ~~^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
4 errors generated.

I have

  • Installed Xcode 10
  • Installed the latest developer command line tools
  • I am on gcc 8.2 (the latest brew provides)
  • followed the suggestions here, which included running...

    open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
    
  • Restarted computer many times

Out of ideas. Compiling c++ from Xcode directly worked. But from command line g++ fails repeatedly. Not sure where to go next.

If relevant this is on MacBook Pro 15", 2018. MacOS 10.14.0.

Thanks for anyone's time.

Edit:

Using the information here I can compile some things using the

-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk

flag in my g++ calls.

Of course I don't want to need to do this forever.

I have COMPLETELY removed Xcode 10 and devtools, and after reinstalling them the issue persists.

What else should I do here? How would it affect only me?

FINAL EDIT

Ended up reinstalling OS to solve issue. Was a big hammer for who knows how big of a nail, but it worked.


回答1:


I had the same problem.

I had the headers installed in /Applications/.../MacOSX10.14.sdk/usr/include. Instead of aliasing my g++ calls to always use -isysroot, I added /Applications/.../MacOSX10.14.sdk/usr/include to my CPLUS_INCLUDE_PATH.

The exact line of code I used and added to my .bash_profile was

export CPLUS_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include

I feel that this is a more elegant solution, since from what I understand using isysroot or sysroot will tell g++ to not search in the default places, when you may need includes from both the mac SDK directory and the usual /usr/include, /usr/local/include, and so on.




回答2:


I believe there is an issue with the directories as the compiler can't find the iostream.h header file in the present working directory, and this might help Error: "no member named 'uint8_t' in the global namespace" on MacOS, though I am not sure.



来源:https://stackoverflow.com/questions/52581857/cant-compile-c-hello-world-with-include-after-upgrading-to-mojave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!