架构x86_64找不到符号 - Cmake - Mac Sierra

8

最近我开始了一个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

搜索所有源文件中的符号。然后验证包含该符号的源文件是否已编译和链接。还要验证符号不是静态或私有的。 - Thomas Matthews
我在您提供的代码中没有看到calculateMethylation函数的定义,只有在parsing/methylation.h中对该函数的声明 - Tsyvarev
你展示的是与calculateMethylationByContext相关的内容,但你的错误与calculateMethylation相关。你在哪里定义了这个函数? - nos
2个回答

6

你的 CMakeLists.txt 没问题。

正如 @thomas-matthews、@tsyvarev 和 @nos 在他们的评论中指出的那样,你的示例代码缺少 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构建错误时,第一步是使用VERBOSE变量设置为true值运行,即: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在幕后所做的工作。

0

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接