2016-11-22 52 views
0

最近我開始了一個新的C++項目。問題是,當我嘗試編譯它時,我得到一個鏈接錯誤。今天我花了整整一天的時間來嘗試調試它,但我並沒有在任何地方找到一個好的解決方案。如果有人可以幫助它,這將是驚人的。我正在使用Mac Sierra。找不到架構x86_64的符號 - Cmake - 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命令工作正常,但是當我然後點擊「使」我得到以下錯誤:

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 
+0

搜索*符號的所有*源文件。然後驗證包含該符號的源文件是編譯*還是鏈接*。同時驗證這些符號不是靜態的或私有的。 –

+0

我沒有在您提供的代碼中看到'calculateMethylation'的**定義**; 'parsing/methylation.h'中只有*聲明*這個函數。 – Tsyvarev

+0

您正在顯示與'calculateMethylationByContext'相關的內容,但是您的錯誤與'calculateMethylation'有關。你在哪裏定義了這個功能? – nos

回答

0

仔細檢查該編譯器是由CMake的自動檢測。 你可以發佈,CMAKE在最初運行CMAKE時告訴你什麼?

This may be a hint for your problem, too

+0

嘿!剛剛發佈了我的cmake命令的輸出。 有趣的是,當我將methylation.cpp文件的內容複製到main.cpp中時,代碼編譯時沒有任何錯誤。看起來鏈接不能正常工作。 – tomooka

+0

你可以檢查,當您嘗試以下會發生什麼: cmake的-DCMAKE_C_COMPILER =的/ usr/bin中/ GCC -DCMAKE_CXX_COMPILER =的/ usr/bin中/ G ++ –

+0

完全同樣的錯誤:( – tomooka

0

CMakeLists.txt是好的。

As @ 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中生成錯誤的第一步是生成Makefiles,將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的調試意想不到的構建失敗的第一步是運行:

❯ make VERBOSE=1 

這會給你瞭解CMake在幕後做了些什麼。

相關問題