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

tom*_*oka 8 c++ cmake linkage undefined-symbol

最近我开始了一个 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
Run Code Online (Sandbox Code Playgroud)

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 ...
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

我使用 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
Run Code Online (Sandbox Code Playgroud)

我的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})
Run Code Online (Sandbox Code Playgroud)

当我运行 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
Run Code Online (Sandbox Code Playgroud)

neg*_*ega 6

你的CMakeLists.txt很好。

\n\n

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

\n\n
\xe2\x9d\xaf make\n[ 33%] Building CXX object CMakeFiles/src.dir/parsing/methylation.cpp.o\n/Users/nega/foo/parsing/methylation.cpp:8:5: warning: control reaches end of non-void function [-Wreturn-type]\n    }\n    ^\n1 warning generated.\n[ 66%] Building CXX object CMakeFiles/src.dir/main.cpp.o\n[100%] Linking CXX executable src\nUndefined symbols for architecture x86_64:\n  "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:\n      _main in main.cpp.o\nld: symbol(s) not found for architecture x86_64\nclang: error: linker command failed with exit code 1 (use -v to see invocation)\nmake[2]: *** [src] Error 1\nmake[1]: *** [CMakeFiles/src.dir/all] Error 2\nmake: *** [all] Error 2\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

假设你有一个正确的实现

\n\n

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

\n\n
\xe2\x9d\xaf make VERBOSE=1\n/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\n/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\n/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all\n/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/src.dir/build.make CMakeFiles/src.dir/depend\ncd /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=\n/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/src.dir/build.make CMakeFiles/src.dir/build\n[ 33%] Building CXX object CMakeFiles/src.dir/parsing/methylation.cpp.o\n/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\n/Users/nega/foo/parsing/methylation.cpp:8:5: warning: control reaches end of non-void function [-Wreturn-type]\n    }\n    ^\n1 warning generated.\n[ 66%] Building CXX object CMakeFiles/src.dir/main.cpp.o\n/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\n[100%] Linking CXX executable src\n/usr/local/Cellar/cmake/3.7.0/bin/cmake -E cmake_link_script CMakeFiles/src.dir/link.txt --verbose=1\n/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 \nUndefined symbols for architecture x86_64:\n  "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:\n      _main in main.cpp.o\nld: symbol(s) not found for architecture x86_64\nclang: error: linker command failed with exit code 1 (use -v to see invocation)\nmake[2]: *** [src] Error 1\nmake[1]: *** [CMakeFiles/src.dir/all] Error 2\nmake: *** [all] Error 2\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

概括

\n\n

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

\n\n
\xe2\x9d\xaf make VERBOSE=1\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n


小智 0

仔细检查 CMAKE 自动检测到哪个编译器。你能发布一下,当你最初运行 CMAKE 时 CMAKE 告诉你什么吗?

这也可能是您问题的提示