2015-01-04 66 views
0

我正在編寫一個小編譯器,但現在我卡住了cmake,它不會生成所有依賴目標文件。CMake不會生成依賴目標文件

只有main.cpp編譯:

#include <iostream> 
#include "lexer.h" 
#include "parser.h" 

int main() { 
    Lexer lexer(&std::cin); 
    Parser parser(&lexer); 

    parser.binaryOpPrecedence['<'] = 10; 
    parser.binaryOpPrecedence['+'] = 20; 
    parser.binaryOpPrecedence['-'] = 20; 
    parser.binaryOpPrecedence['*'] = 40; 

    std::cerr << "ready> "; 
    parser.getNextToken(); 

    parser.parse(); 

    return 0; 
} 

CMakeLists.txt如下(子目錄):

set(SOURCE_FILES 
    ${PROJECT_SOURCE_DIR}/src/ast/binary.h 
    ${PROJECT_SOURCE_DIR}/src/ast/call.h 
    ${PROJECT_SOURCE_DIR}/src/ast/expression.h 
    ${PROJECT_SOURCE_DIR}/src/ast/function.h 
    ${PROJECT_SOURCE_DIR}/src/ast/number.h 
    ${PROJECT_SOURCE_DIR}/src/ast/prototype.h 
    ${PROJECT_SOURCE_DIR}/src/ast/variable.h 
    ${PROJECT_SOURCE_DIR}/src/lexer.cpp 
    ${PROJECT_SOURCE_DIR}/src/parser.cpp 
    PARENT_SCOPE 
) 

add_executable(kaleidoscope main.cpp ${SOURCE_FILES}) 

至於說,只有main.cpp被編譯到main.cpp.o但不lexer.cppparser.cpp,因此鏈接崩潰與未解決的符號。不知道,我做錯了什麼。

/usr/bin/cmake --build /home/patkar/.clion10/system/cmake/generated/d6bd25de/d6bd25de/Debug --target kaleidoscope -- -j 4 
Scanning dependencies of target kaleidoscope 
[100%] Building CXX object src/CMakeFiles/kaleidoscope.dir/main.cpp.o 
Linking CXX executable kaleidoscope 
CMakeFiles/kaleidoscope.dir/main.cpp.o: In function `Lexer': 
/home/patkar/workspace/compiler-fun/kaleidoscope/src/lexer.h:49: undefined reference to `vtable for Lexer' 
CMakeFiles/kaleidoscope.dir/main.cpp.o: In function `main': 
/home/patkar/workspace/compiler-fun/kaleidoscope/src/main.cpp:36: undefined reference to `Parser::parse()' 
CMakeFiles/kaleidoscope.dir/main.cpp.o: In function `~Lexer': 
/home/patkar/workspace/compiler-fun/kaleidoscope/src/lexer.h:38: undefined reference to `vtable for Lexer' 
/home/patkar/workspace/compiler-fun/kaleidoscope/src/lexer.h:38: undefined reference to `vtable for Lexer' 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 
make[3]: *** [src/kaleidoscope] Error 1 
make[2]: *** [src/CMakeFiles/kaleidoscope.dir/all] Error 2 
make[1]: *** [src/CMakeFiles/kaleidoscope.dir/rule] Error 2 
make: *** [kaleidoscope] Error 2 
+0

您確認'SOURCE_FILES'設置爲正確的路徑嗎? – 2015-01-04 11:25:46

+0

不是直接的,但是路徑是正確的,因爲我也在測試目標中使用了'SOURCE_FILES',它在那裏工作。就是這樣,'PARENT_SCOPE'在當前範圍內使變量不可訪問/爲空? – 2015-01-04 11:50:37

+1

是的,我遇到了同樣的問題。不幸的是,[documentation](http://www.cmake.org/cmake/help/v3.0/command/set.html)也沒有提供這種行爲的任何規範。你可以通過在父範圍中設置它,它也可以在當前範圍內訪問。 – 2015-01-04 12:01:57

回答

1

好吧,這是使我SOURCE_FILES變量無法進入/在當前範圍內空PARENT_SCOPE:o

我需要爲我的測試目標訪問那裏所有的源文件。我現在使用PARENT_SCOPE作爲解決方法創建了一個新變量。

set(SOURCE_FILES 
    ${PROJECT_SOURCE_DIR}/src/ast/binary.h 
    ${PROJECT_SOURCE_DIR}/src/ast/call.h 
    ${PROJECT_SOURCE_DIR}/src/ast/expression.h 
    ${PROJECT_SOURCE_DIR}/src/ast/function.h 
    ${PROJECT_SOURCE_DIR}/src/ast/number.h 
    ${PROJECT_SOURCE_DIR}/src/ast/prototype.h 
    ${PROJECT_SOURCE_DIR}/src/ast/variable.h 
    ${PROJECT_SOURCE_DIR}/src/lexer.cpp 
    ${PROJECT_SOURCE_DIR}/src/parser.cpp 
) 

set(SOURCE_TEST_FILES ${SOURCE_FILES} PARENT_SCOPE) 

add_executable(kaleidoscope main.cpp ${SOURCE_FILES}) 
+0

我想你的意思'add_executable(萬花筒的main.cpp $ {} SOURCE_TEST_FILES)':P – 2015-01-04 12:02:48

+1

沒有,'add_executable(萬花筒的main.cpp $ {} SOURCE_FILES)'是正確的。 'SOURCE_TEST_FILES'是具有'PARENT_SCOPE'來在測試可執行文件中訪問它的人。 – 2015-01-04 12:24:33