src / libdyld / MachOObject.cpp中的编译错误;编译亲爱的

时间:2013-10-09 08:04:05

标签: c++ compilation

我正在编译亲爱的,并且在libdyld

中编译MachOObject.cpp时失败了

我得到的错误

MachOObject.cpp:534:20: error: expected primary-expression before ‘void’
MachOObject.cpp:534:20: error: expected ‘)’ before ‘void’

编译器的错误与此有关

bool MachOObject::lookupDyldFunction(const char* name, void** addr)
{
LOG << "lookupDyldFunction: " << name << std::endl;

*addr = dlsym(RTLD_DEFAULT, name);

if (!*addr)
    *addr = (void*) (void (*)()) []() { LOG << "Fake dyld function called\n"; };

return (*addr) != 0;

}

请注意,这是在void函数和命名空间中的函数,而这不是我自己编写的

阅读整篇文章,你会发现这是在c ++中

所有相关代码(已移除批量)

http://pastebin.com/raw.php?i=j6kkkVee

github上的整个程序

https://github.com/LubosD/darling

1 个答案:

答案 0 :(得分:1)

好的,代码是C ++ 11代码,Fake dlyd function是lambda,仅支持clang 3.1及以上版本,g ++ 4.5及更新版本。请注意,我无法使用系统附带的g ++ 4.6.2编译scce。

基于自述文件和如何构建代码的文档,您需要使用clang 3.1或更新版本,并且必须使用-std=c++11标志进行编译。如果您正在使用完全不支持该语法的编译器(比如,看起来,您正在使用的编译器),那么您将看到此错误。

在这种情况下,您可以对代码进行一些小修改,以便将虚函数移到方法之外,只是对它进行引用以便编译,但这意味着您仍然使用未经测试的编译器和一个开发人员没有称之为支持机制。

这个scce将是:

#include <iostream>
#include <dlfcn.h>

bool lookupDyldFunction(const char* name, void** addr)
{
    std::cerr << "lookupDyldFunction: " << name << std::endl;

    *addr = dlsym(RTLD_DEFAULT, name);

    if (!*addr)
        *addr = (void*) (void (*)()) []() { std::cerr << "Fake dyld function called\n"; };

    return (*addr) != 0;
}

使用g ++编译:

$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -std=c++0x -c lookup.cpp
lookup.cpp: In function ‘bool lookupDyldFunction(const char*, void**)’:
lookup.cpp:12:26: error: expected primary-expression before ‘void’
lookup.cpp:12:26: error: expected ‘)’ before ‘void’

使用clang ++编译(5.0,在mac上;我没有新的linux clang ATM):

$ clang --version
Apple LLVM version 5.0 (clang-500.2.78) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
$ clang++ -std=c++11 -c lookup.cpp
$

即。编译成功了。

请注意,CMakeList.txt文件中没有任何内容指定/需要编译器;它只是确保将-std=c++11传递给编译器。