简体   繁体   English

由于 __GLIBC__ 问题,无法在 macbook 中编译 c++ 代码

[英]unable to compile c++ code in macbook due to __GLIBC__ issue

g++ -std=c++11 -Wall -g threads.cpp -o threads.out g++ -std=c++11 -Wall -g threads.cpp -o threads.out

In file included from threads.cpp:1:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:37:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:215:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:401:32: error: use of undeclared identifier '_ISspace'
    static const mask space  = _ISspace;
                               ^
Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:402:32: error: use of undeclared identifier '_ISprint'
    static const mask print  = _ISprint;
                               ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:403:32: error: use of undeclared identifier '_IScntrl'
    static const mask cntrl  = _IScntrl;
                               ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:404:32: error: use of undeclared identifier '_ISupper'
    static const mask upper  = _ISupper;
                               ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:405:32: error: use of undeclared identifier '_ISlower'
    static const mask lower  = _ISlower;
                               ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:406:32: error: use of undeclared identifier '_ISalpha'
    static const mask alpha  = _ISalpha;

can you please help me to resolve this __locale issue while compiling the c++ code.在编译 c++ 代码时,您能帮我解决这个 __locale 问题吗?

#include <iostream>
#include <thread>
using namespace std;

void fun(void)
{
    cout << "Vaule " << 10 << endl;
}

int main()
{
    thread t1(fun);
    thread t2(fun);
    return 0;
}

compiling command: g++ -std=c++11 -Wall -g thread.cpp -o thread.out编译命令:g++ -std=c++11 -Wall -g thread.cpp -o thread.out

Two things fix the problem you're having.有两件事可以解决您遇到的问题。

Thing the first, add the compiler option -pthread .首先,添加编译器选项-pthread My compile command: clang++ -Wall -Wextra -std=c++11 -pthread main.cpp我的编译命令: clang++ -Wall -Wextra -std=c++11 -pthread main.cpp

Thing the second, join your threads before ending the program.第二件事,在结束程序之前加入你的线程。

    t1.join();
    t2.join();

Something it does not fix, is that your std::cout statement will likely be jumbled because the threads simply dump their data into the single stream as they please.它无法解决的问题是您的std::cout语句可能会混乱,因为线程只是随意将其数据转储到单个 stream 中。 For an example, my output was the following:例如,我的 output 如下:

Vaule Vaule 10
10

In order to fix that, you'll likely need to place a lock(mutex) around the std::cout statement.为了解决这个问题,您可能需要在std::cout语句周围放置一个 lock(mutex) 。

As I said in my comment, I do not recommend using g++ unless you installed it yourself.正如我在评论中所说,除非您自己安装,否则我不建议使用g++ The command you're using is an alias that doesn't behave, because of some text you left out.您正在使用的命令是一个不起作用的别名,因为您遗漏了一些文本。

❯ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Contrast with clang++与clang++对比

❯ clang++ --version
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Of note in the g++ section is the 'Configured with' line. g++ 部分中值得注意的是“配置为”行。 It uses a Standard Library from gcc 4.2.1, which is pre-C++11.它使用来自 C++11 之前的 gcc 4.2.1 的标准库。 You should not have left that information out.您不应该遗漏这些信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM