CMake 不生成依赖对象文件
CMake doesn't generate depend object files
我正在编写一个小编译器,但现在我被 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.cpp
和 parser.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
好的,正是 PARENT_SCOPE
使我的 SOURCE_FILES
变量成为当前范围内的 inaccessible/empty :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})
我正在编写一个小编译器,但现在我被 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.cpp
和 parser.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
好的,正是 PARENT_SCOPE
使我的 SOURCE_FILES
变量成为当前范围内的 inaccessible/empty :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})