未找到架构 x86_64 的符号 - Cmake - Mac sierra

Posted

技术标签:

【中文标题】未找到架构 x86_64 的符号 - Cmake - Mac sierra【英文标题】:Symbol(s) not found for architecture x86_64 - Cmake - Mac sierra 【发布时间】:2016-11-22 15:58:03 【问题描述】:

最近我开始了一个 C++ 的新项目。问题是,当我尝试编译它时,我得到一个链接错误。我今天花了一整天的时间尝试调试它,但我并没有在任何地方找到一个好的解决方案。如果有人可以提供帮助,那就太棒了。我使用的是 Mac Sierra。

parsing/methylation.h

#ifndef EPIRL_METHYLATION_H
#define EPIRL_METHYLATION_H

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>

using namespace std;

namespace methylation 
    struct MethLine 
        string chr;
        int coord;
        char strand;
        int methylated;
        int un_methylated;
        string context;
        string tag;
    ;

    string calculateMethylationByContext(
            MethLine m_input[], int length,
            int window_start, int window_end, int threshold);

    void calculateMethylation(
        const istream &methylation_stream,
        const istream &coordinate_stream,
        const ostream &output_stream
    );


#endif //EPIRL_METHYLATION_H

parsing/methylation.cpp

#include "methylation.h"

namespace methylation 
    string calculateMethylationByContext(
            MethLine m_input[], int length,
            int window_start, int window_end, int threshold) 
// rest of the code ...
    

main.cpp

#include <iostream>
#include <fstream>
#include "parsing/methylation.h"

using namespace std;

int main(int argc, char **argv) 
    if (argc != 4) 
        cout << "Invalid number of arguments..." << endl;
        return 1;
    

    char *methylation_file = argv[1];
    char *coordinate_file = argv[2];
    char *output_file = argv[3];

    ifstream methylation_file_stream(methylation_file, ios::binary);
    ifstream coordinate_file_stream(coordinate_file, ios::binary);
    ofstream output_file_stream(output_file, ios::binary);

    methylation::calculateMethylation(methylation_file_stream,
                         coordinate_file_stream, output_file_stream);
    methylation_file_stream.close();
    coordinate_file_stream.close();
    output_file_stream.close();

    return 0;

我使用 CLion 进行编码。当我尝试构建它时,我的 cmake 命令工作正常,但是当我单击“make”时,我收到以下错误:

Undefined symbols for architecture x86_64:
  "methylation::calculateMethylation(std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_ostream<char, std::__1::char_traits<char> > const&)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [src] Error 1
make[2]: *** [CMakeFiles/src.dir/all] Error 2
make[1]: *** [CMakeFiles/src.dir/rule] Error 2
make: *** [src] Error 2

我的 CMakeLists.txt 文件如下所示:

cmake_minimum_required(VERSION 3.6)
project(src)

set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS -std=c++11")

set(SOURCE_FILES
    parsing/methylation.cpp
    parsing/methylation.h
    main.cpp)

add_executable(src $SOURCE_FILES)

当我运行 cmake 命令时,我的输出是这样的:

-- The C compiler identification is AppleClang 8.0.0.8000042
-- The CXX compiler identification is AppleClang 8.0.0.8000042
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/sztankatt/Documents/University/PartIII/Project/epiRL/src

【问题讨论】:

在您的所有源文件中搜索符号。然后验证包含该符号的源文件是否已编译并链接。还要验证符号不是静态的或私有的。 我在您提供的代码中没有看到calculateMethylation定义parsing/methylation.h 中只有此函数的声明 您正在显示与 calculateMethylationByContext 相关的内容,但您的错误与 calculateMethylation 相关。你在哪里定义了这个函数? 【参考方案1】:

您的CMakeLists.txt 很好。

正如@thomas-matthews @tsyvarev @nos 在他们的 cmets 中指出的那样,您的示例代码缺少 methylation::calculateMethylation() 的定义/实现。您看到的是 Apple/clang 在这种情况下的预期失败。

❯ make
[ 33%] Building CXX object CMakeFiles/src.dir/parsing/methylation.cpp.o
/Users/nega/foo/parsing/methylation.cpp:8:5: warning: control reaches end of non-void function [-Wreturn-type]
    
    ^
1 warning generated.
[ 66%] Building CXX object CMakeFiles/src.dir/main.cpp.o
[100%] Linking CXX executable src
Undefined symbols for architecture x86_64:
  "methylation::calculateMethylation(std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_ostream<char, std::__1::char_traits<char> > const&)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [src] Error 1
make[1]: *** [CMakeFiles/src.dir/all] Error 2
make: *** [all] Error 2

添加一个虚拟实现,或在您的main.cpp 中注释掉调用,将允许make 成功完成。

假设你有一个正确的实现

假设您的代码中确实有methylation::calculateMethylation() 的实现(可能在另一个文件中)。在 CMake 生成的 Makefile 中调试构建错误的第一步是运行 make 变量 VERBOSE 设置为真值,即:make VERBOSE=1

❯ make VERBOSE=1
/usr/local/Cellar/cmake/3.7.0/bin/cmake -H/Users/nega/foo -B/Users/nega/foo/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.7.0/bin/cmake -E cmake_progress_start /Users/nega/foo/build/CMakeFiles /Users/nega/foo/build/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/src.dir/build.make CMakeFiles/src.dir/depend
cd /Users/nega/foo/build && /usr/local/Cellar/cmake/3.7.0/bin/cmake -E cmake_depends "Unix Makefiles" /Users/nega/foo /Users/nega/foo /Users/nega/foo/build /Users/nega/foo/build /Users/nega/foo/build/CMakeFiles/src.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/src.dir/build.make CMakeFiles/src.dir/build
[ 33%] Building CXX object CMakeFiles/src.dir/parsing/methylation.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++     -std=gnu++11 -o CMakeFiles/src.dir/parsing/methylation.cpp.o -c /Users/nega/foo/parsing/methylation.cpp
/Users/nega/foo/parsing/methylation.cpp:8:5: warning: control reaches end of non-void function [-Wreturn-type]
    
    ^
1 warning generated.
[ 66%] Building CXX object CMakeFiles/src.dir/main.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++     -std=gnu++11 -o CMakeFiles/src.dir/main.cpp.o -c /Users/nega/foo/main.cpp
[100%] Linking CXX executable src
/usr/local/Cellar/cmake/3.7.0/bin/cmake -E cmake_link_script CMakeFiles/src.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/src.dir/parsing/methylation.cpp.o CMakeFiles/src.dir/main.cpp.o  -o src 
Undefined symbols for architecture x86_64:
  "methylation::calculateMethylation(std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_ostream<char, std::__1::char_traits<char> > const&)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [src] Error 1
make[1]: *** [CMakeFiles/src.dir/all] Error 2
make: *** [all] Error 2

现在您可以查看链接步骤,看看您是否缺少项目;可能是一个库,或者一个目标文件。如果是这样,现在您知道返回并将其添加到您的CMakeLists.txt

总结

使用 CMake 生成的 Makefile 调试意外构建失败的第一步是运行:

❯ make VERBOSE=1

这将使您深入了解 CMake 在幕后所做的事情。

【讨论】:

【参考方案2】:

仔细检查 CMAKE 自动检测到的编译器。 您能否发布一下,当您最初运行 CMAKE 时 CMAKE 会告诉您什么?

This may be a hint for your problem, too

【讨论】:

嘿!刚刚发布了我的 cmake 命令的输出。有趣的是,当我将甲基化.cpp 文件的内容复制到 main.cpp 中时,代码编译时没有任何错误。链接似乎无法正常工作。 您能否检查一下当您尝试以下操作时会发生什么:cmake -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++ 您可以尝试执行以下操作: target_link_libraries(src stdc++) 这应该确保您的项目链接到 c++ 标准库... 顺便说一句:有时在执行另一个 cmake 生成调用之前清理 CMAKE 构建目录 (rm -rf *) 会很有帮助,因为 CMAKE 有时会保留缓存的变量

以上是关于未找到架构 x86_64 的符号 - Cmake - Mac sierra的主要内容,如果未能解决你的问题,请参考以下文章

AFNetworking 错误 - AFHTTPRequestOperationManager(未找到架构 x86_64 的符号)

ld:未找到架构 x86_64(领域)的符号

Qt5“未找到架构 x86_64 的符号”

C++ 库编程错误:ld:未找到架构 x86_64 的符号

架构 x86_64 / i386 的未定义符号

错误:未找到架构 x86_64 的符号,collect2:ld 返回 1 退出状态