2016-11-22 4 views
0

Kürzlich habe ich ein neues Projekt in C++ gestartet. Das Problem ist, wenn ich versuche, es zu kompilieren, bekomme ich einen Verbindungsfehler. Ich habe heute den ganzen Tag damit verbracht, es zu debuggen, aber ich habe nirgends eine gute Lösung gefunden. Wenn jemand damit helfen könnte, wäre es erstaunlich. Ich benutze einen Mac Sierra.Symbol (e) nicht gefunden für Architektur 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; 
} 

Ich verwende CLion für meine Codierung. Wenn ich versuche, es zu bauen, mein Cmake Befehl funktioniert gut, aber wenn ich dann ‚make‘ ich auf die folgende Fehlermeldung erhalten:

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 

meine CMakeLists.txt Datei sieht wie folgt aus:

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}) 

Wenn ich den Cmake Befehl ausführen, ist mein Ausgang dieses:

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

Suchen Sie * alle * Ihrer Quelldateien nach dem Symbol. Stellen Sie dann sicher, dass die Quelldatei mit dem Symbol kompiliert * und verknüpft * ist. Stellen Sie außerdem sicher, dass die Symbole nicht statisch oder privat sind. –

+0

Ich sehe ** Definition ** der 'calcitMethylation' in dem von Ihnen bereitgestellten Code nicht; Es gibt nur * Deklaration * dieser Funktion in der 'parsing/methylation.h'. – Tsyvarev

+0

Sie zeigen Sachen an, die sich auf 'calculateMethylylationByContext' beziehen, aber Ihr Fehler bezieht sich auf' calculateMethylation'. Wo haben Sie diese Funktion definiert? – nos

Antwort

0

Überprüfen Sie die Compiler automatisch von CMAKE erkannt wird. Können Sie posten, was CMAKE Ihnen sagt, wenn Sie anfänglich CMAKE ausführen?

This may be a hint for your problem, too

+0

Hey! Habe gerade die Ausgabe meines cmake-Befehls gepostet. Interessant, wenn ich den Inhalt der methylation.cpp-Datei in die main.cpp kopieren, kompiliert der Code ohne Fehler. Es scheint, dass die Verknüpfung nicht angemessen funktioniert. – tomooka

+0

Können Sie überprüfen, was passiert, wenn Sie versuchen, folgende: Cmake -DCMAKE_C_COMPILER =/usr/bin/gcc -DCMAKE_CXX_COMPILER =/usr/bin/g ++ –

+0

Genau das gleiche Fehler :( – tomooka

0

Ihre CMakeLists.txt ist in Ordnung.

Wie @thomas-matthews @tsyvarev @nos in ihren Kommentaren darauf hingewiesen, fehlt Ihrem Beispielcode die Definition/Implementierung von . Was Sie sehen, ist der erwartete Fehler mit Apple/Clang in dieser Situation.

❯ 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 

Hinzufügen eines Dummy-Implementierung oder den Anruf in Ihrem main.cpp Auskommen wird make erlauben erfolgreich abzuschließen.

Angenommen, Sie haben eine korrekte Umsetzung

Nehmen wir an, Sie eine Implementierung von methylation::calculateMethylation() im Code tun haben (vielleicht in einer anderen Datei). Der erste Schritt beim Debuggen von Build-Fehlern in CMake-generierten Makefiles besteht darin, dass die make-Variable VERBOSE auf einen wahren Wert gesetzt wird, dh: 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 

Jetzt können Sie den Link Schritt betrachten und sehen, ob Sie Elemente fehlen; vielleicht eine Bibliothek oder eine Objektdatei.Wenn ja, jetzt wissen Sie, zurück zu gehen und fügen Sie sie CMakeLists.txt

Zusammenfassung

Der erste Schritt beim Debuggen unerwarteten Build-Fehlern mit CMake Makefiles erzeugt wird, auszuführen:

❯ make VERBOSE=1 

Dies gibt Sie Einblick in was CMake hinter den Kulissen tut.

Verwandte Themen