带有提升“语义问题 - 未声明标识符 va_start”的 XCode

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12573271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:37:25  来源:igfitidea点击:

XCode with boost "Semantic Issue - undeclared identifier va_start"

c++xcodemacosboost

提问by Paulo Henrique

C++locale.h
->Semantic Issue
-->Use of undeclared identifier 'va_start'
->Semantic Issue
-->Use of undeclared identifier 'va_end'

First time using boost, downloaded it using ports and created a command line project in XCode. Header Search Path: /usr/include/**

第一次使用boost,使用ports 下载并在XCode 中创建了一个命令行项目。标题搜索路径:/usr/include/**

There is nothing in the code yet, just the main function that comes with the default proj.

代码中还没有任何内容,只有默认项目附带的主要功能。

Just don't know what to do, never expected this to happen.

只是不知道该怎么办,没想到会发生这种情况。

EDIT1:

编辑1:

First occurrence:

第一次出现:

#ifndef _GLIBCXX_CSTDARG
#define _GLIBCXX_CSTDARG 1

#pragma GCC system_header

#include <bits/c++config.h>
#include <stdarg.h>

// Adhere to section 17.4.1.2 clause 5 of ISO 14882:1998
#ifndef va_end
#define va_end(ap) va_end (ap)
#endif

_GLIBCXX_BEGIN_NAMESPACE(std)

  using ::va_list;

_GLIBCXX_END_NAMESPACE

#endif

It's a file without extension in \usr\include\c++\4.2.1 and i just realized that this file has nothing to do with boost, there is something nasty happening here.

这是一个在 \usr\include\c++\4.2.1 中没有扩展名的文件,我刚刚意识到这个文件与 boost 无关,这里发生了一些令人讨厌的事情。

EDIT2: After fixing the include dir to /opt/local/include/** new errors appeared:

EDIT2:将包含目录修复为 /opt/local/include/** 后,出现了新错误:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1/type_traits:214:46:
Use of undeclared identifier 'nullptr_t'; did you mean 'nullptr'?

There are other errors, all related to these files in the folder sr/lib/c++/v1/ why is that? These file seems to be some core functionality, they can't be broke.

还有其他错误,都与文件夹sr/lib/c++/v1/中的这些文件有关,这是为什么?这些文件似乎是一些核心功能,它们不能被破坏。

Here is a pic of the errors, maybe ou guys see something Errors

这是错误的图片,也许你们看到了什么 错误

EDIT3: Changing the compiler from Apple LLVM to GCC LLVM reduces the errors to only one: "vspintf is not a member of 'std'" in c++locale.h. Ok, now I'm completely lost.

EDIT3:将编译器从 Apple LLVM 更改为 GCC LLVM 将错误减少到只有一个:c++locale.h 中的“vspintf 不是‘std’的成员”。好吧,现在我完全迷失了。

采纳答案by abarnert

First, you're may not be #includeing the right headers.

首先,您可能没有#include输入正确的标题。

For example, if you don't #include <cstddef>, you can't use std::nullptr_t; attempting to do so will give you:

例如,如果不这样做#include <cstddef>,则不能使用std::nullptr_t; 尝试这样做会给你:

Semantic issue: Use of undeclared identifier 'nullptr_t'. Did you mean 'nullptr'?

Often, pulling in one header happens to implicitly pull in various other headers, so things work even when they shouldn't. libstdc++ does this a lot more than libc++, and the former is the default library for llvm-g++ ("GCC LLVM"), the latter for clang ("Apple LLVM"), which means a lot of errors will seem to go away when you switch to "GCC LLVM" (or just stick with "Apple LLVM" and switch your library), but your code is still wrong.

通常,拉入一个标头恰好隐含地拉入了其他各种标头,因此即使不应该这样做也能正常工作。libstdc++ 比 libc++ 做得更多,前者是 llvm-g++(“GCC LLVM”)的默认库,后者是 clang(“Apple LLVM”)的默认库,这意味着很多错误似乎会在您切换到“GCC LLVM”(或者只是坚持使用“Apple LLVM”并切换您的库),但您的代码仍然是错误的。

Another possibility is that you're trying to compile C++11 in C++03 mode. For example, when you #include <cstddef>in C++03 mode, it shouldn't define nullptr_t. It might do so anyway—and it might do so with llvm-g++ and/or libstdc++, but not with clang and/or libc++. But your code (or, in this case, your project) is still wrong and should be fixed.

另一种可能性是您尝试在 C++03 模式下编译 C++11。例如,当您#include <cstddef>处于 C++03 模式时,它不应该定义nullptr_t. 无论如何,它可能会这样做——它可能会使用 llvm-g++ 和/或 libstdc++ 这样做,但不会使用 clang 和/或 libc++。但是你的代码(或者,在这种情况下,你的项目)仍然是错误的,应该修复。

If you can show us the actual (stripped-down) code that generates these errors, and tell us exactly what settings you changed from the default "Command Line Tool, C++" project you created in Xcode (assuming that's what you created), it should be possible to be more specific.

如果您能向我们展示产生这些错误的实际(精简)代码,并准确地告诉我们您从您在 Xcode 中创建的默认“命令行工具,C++”项目(假设这就是您创建的)中更改的设置,它应该可以更具体一些。

To get a minimal program working with boost, assuming you've got Xcode, MacPorts, and MacPorts boost installed properly, all you have to do is this:

为了让最小的程序与 boost 一起工作,假设你已经正确安装了 Xcode、MacPorts 和 MacPorts boost,你所要做的就是:

  • Create a new Command Line Tool, C++.
  • Click on the project, then under Search Paths, go to Header Search Paths, and edit it to add /opt/local/include.
  • Modify main.cppto look like the following:
  • 创建一个新的命令行工具 C++。
  • 单击该项目,然后在 Search Paths 下,转到 Header Search Paths,并对其进行编辑以添加/opt/local/include.
  • 修改main.cpp为如下所示:

main.cpp

主程序

#include <iostream>
#include <boost/uuid/uuid.hpp>

int main(int argc, const char * argv[])
{
    boost::uuids::uuid u1 = {
        0x12, 0x34, 0x56, 0x78,
        0x90, 0xab, 0xcd, 0xef,
        0x12, 0x34, 0x56, 0x78,
        0x90, 0xab, 0xcd, 0xef };
    boost::uuids::uuid u2 = {
        0x12, 0x34, 0x56, 0x78,
        0x90, 0xab, 0xcd, 0xef,
        0x12, 0x34, 0x56, 0x78,
        0x90, 0xab, 0xcd, 0xef };
    std::cout << (u1 == u2) << "\n";
    return 0;
}

I picked boost::uuid because it's a dead-simple library, but the same should work for compiling any part of boost.

我选择 boost::uuid 是因为它是一个非常简单的库,但同样适用于编译 boost 的任何部分。

Some parts of boost also require you to link in a library. For that, you'll also need to add /opt/local/libto Library Search Paths before you can use most of the usual techniques for adding libraries to a project.

boost 的某些部分还需要您在库中进行链接。为此,您还需/opt/local/lib要先添加到库搜索路径,然后才能使用大多数常用技术将库添加到项目中。

Note that if you're up to date, you can make this even shorter, because with Xcode 4.5, the defaults will be Apple LLVM Compiler 4.1, GNU++11 dialect, and libc++ with C++11 support library. But I made sure my code was C++03-compatible so it works with the defaults in almost any version of Xcode (I tested with 3.2), since you never told us what version you were using.

请注意,如果您是最新的,您可以将其缩短,因为使用 Xcode 4.5,默认值将是 Apple LLVM 编译器 4.1、GNU++11 方言和带有 C++11 支持库的 libc++。但是我确保我的代码与 C++03 兼容,因此它可以与几乎任何版本的 Xcode(我用 3.2 测试)的默认值一起使用,因为您从未告诉我们您使用的是哪个版本。

回答by icapurro

I had the same issue, I've installed Boost with homebrew and when I add the 'Header Search Path' (/usr/local/Cellar/boost/1.54.0/include with the recursive option) in XCode the build throw those errors.

我遇到了同样的问题,我已经用自制软件安装了 Boost,当我在 XCode 中添加“标题搜索路径”(/usr/local/Cellar/boost/1.54.0/include 和递归选项)时,构建会抛出这些错误.

To fix it I changed the recursive option to non-recursive on the 'Header Search Path' and it worked.

为了修复它,我将“标题搜索路径”上的递归选项更改为非递归,并且它起作用了。

回答by Nathan

I had this problem and solved it. In XCode on osx mavericks had to add /opt/local/libs/ to the library search path (non-recursive). I then added /opt/local/include/ to the header search path (also non-recursive).

我遇到了这个问题并解决了它。在 osx Mavericks 上的 XCode 中,必须将 /opt/local/libs/ 添加到库搜索路径(非递归)。然后我将 /opt/local/include/ 添加到标头搜索路径(也是非递归的)。

回答by Adam Rosenfield

You need to #include <stdarg.h>in order to use the va_startmacros et al. If those errors are happening in a header file, then that header file should include <stdarg.h>; if it doesn't, you can work around it by including it yourself beforeyou include the problematic header (but you should also report the issue to the library developers, if possible).

你需要#include <stdarg.h>为了使用va_start宏等。如果这些错误发生在头文件中,则该头文件应包含<stdarg.h>; 如果没有,您可以包含有问题的标头之前通过自己包含它来解决它(但如果可能,您也应该向库开发人员报告该问题)。

回答by abarnert

From the latest comment, I think I know the problem.

从最新的评论来看,我想我知道问题所在。

Header Search Path: /usr/include/**

Just created another fresh XCode command line tool app. There is no errors at all when building the Hello World, but just adding the Header Search Path breaks the compilation with the above stated error, there is no reference to boost in my code yet, just added the search path.

标题搜索路径:/usr/include/**

刚刚创建了另一个新的 XCode 命令行工具应用程序。构建 Hello World 时根本没有错误,但是仅添加 Header Search Path 就会破坏编译并出现上述错误,我的代码中还没有对 boost 的引用,只是添加了搜索路径。

Where did you get the search path /usr/include/**?

你从哪里得到的搜索路径/usr/include/**

MacPorts installs everything to /opt/local, not /usr, so you want /opt/local/include(or /opt/local/include/**or /opt/local/include/boost); adding /usr/include/**isn't going to help at all with Boost.

MacPorts 将所有内容安装到/opt/local,而不是/usr,因此您想要/opt/local/include(或/opt/local/include/**/opt/local/include/boost);添加/usr/include/**对 Boost 根本没有帮助。

However, it maybreak your code even before you get to Boost.

但是,它甚至可能在您使用 Boost 之前破坏您的代码。

What's in /usr/includeis Xcode's Command Line Tools. If you don't have these, you've got an incomplete and unusable set of headers; if you do, you've got a set of headers that conflicts with SDK-based builds.

里面/usr/include有 Xcode 的命令行工具。如果您没有这些,则您将获得一组不完整且无法使用的标头;如果这样做,您就会得到一组与基于 SDK 的构建冲突的标头。

The answer is to not add /usr/include/**to your search path.

答案是不要添加/usr/include/**到您的搜索路径中。

Or, if you really need to add it (but really, you don't), change the Base SDK to "Current OS X" instead of "Latest OS X", which means you'll be getting your default headers out of /usr/includeinstead of, e.g., /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include, and of course adding a path that's already there won't lead to any conflicts.

或者,如果您真的需要添加它(但实际上您不需要),请将 Base SDK 更改为“Current OS X”而不是“Latest OS X”,这意味着您将使用默认标头/usr/include代替的,例如,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include当然,添加一条已经存在的路径不会导致任何冲突。

回答by jewfro

I seem to have fixed it. After trying installs with homebrew, macports and manually, it was hard to find everything that needed changing in the backend of Xcode, which I admit to still finding a little confusing at times. So Header search paths still contained the homebrew cellar as part of the long pathname, so I changed this bit /usr/include User Header Path I had as /usr/local/lib according to some instructions I'd found searching for a solution. I've now changed it to /usr/local/lib

我好像修好了。在尝试使用自制软件、macports 和手动安装后,很难在 Xcode 的后端找到需要更改的所有内容,我承认有时仍然会感到有些困惑。所以标题搜索路径仍然包含自制酒窖作为长路径名的一部分,所以我根据我在搜索解决方案时找到的一些说明更改了这个位 /usr/include 用户标题路径我作为 /usr/local/lib 。我现在已将其更改为 /usr/local/lib

After these changes, my errors have finally disappeared! Fingers crossed I've got it right now

经过这些更改,我的错误终于消失了!手指交叉我现在就知道了