CMake 链接错误(collect2:ld 返回 1 退出状态)/usr/bin/ld:找不到 -lexpression

CMake linking error (collect2: ld returned 1 exit status) /usr/bin/ld: cannot find -lexpression

提问人:Nithin Varghese 提问时间:8/18/2022 更新时间:8/18/2022 访问量:538

问:

项目结构如下

├── ast
│   ├── CMakeLists.txt
│   ├── expression
│   │   ├── CMakeLists.txt
│   │   ├── Expression.cpp
│   │   ├── Expression.hpp
│   │   ├── NumericExpression.cpp
│   │   └── NumericExpression.h
│   ├── Visitor.cpp
│   └── Visitor.hpp
├── callSlang
│   ├── CMakeLists.txt
│   └── main.cpp
├── CMakeLists.txt
├── contexts
│   ├── CMakeLists.txt
│   ├── Context.hpp
│   ├── Symbol.cpp
│   ├── Symbol.hpp
│   └── SymbolTable.hpp
├── frontend
│   ├── CMakeLists.txt
│   ├── Parser.cpp
│   └── Parser.hpp
├── Makefile
├── meta
│   ├── CMakeLists.txt
│   └── Meta.hpp
└── README.md

CMakeLists.txt

cmake_minimum_required(VERSION 3.16.3)

# set the project name
# https://stackoverflow.com/questions/71740678/cmake-error-in-findterminfo-with-clang-15-on-macos
project(slangLLvm VERSION 1.0 LANGUAGES C CXX)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_BUILD_TYPE "Debug")


set(LLVM_DIR /usr/lib/llvm-14/lib/cmake/llvm)

find_package(LLVM REQUIRED CONFIG
             PATHS ${search_paths}
             NO_DEFAULT_PATH)



add_subdirectory(meta)
add_subdirectory(contexts)
add_subdirectory(ast)
add_subdirectory(frontend)
add_subdirectory(callSlang)

callSlang/CMakeLists.txt

set(SOURCE_FILE main.cpp)
# add the files for creating executable
add_executable(slangLLVM ${SOURCE_FILE})


#link the libs 
target_link_libraries(slangLLVM 
PRIVATE
    ast
    frontend
)

ast/CMakeLists.txt

set(SOURCE_FILES Visitor.cpp)
add_library(ast ${SOURCE_FILES})

target_link_libraries(ast 
PUBLIC 
    meta
    contexts
    LLVMSupport
    expression
PRIVATE
    frontend
)

    
# get the include dirs 
target_include_directories(ast 
INTERFACE  
    .
PUBLIC
    ${LLVM_INCLUDE_DIRS}
    ${PROJECT_SOURCE_DIR}/meta 
    ${PROJECT_SOURCE_DIR}/ast/expression
    
)

add_subdirectory(expression)

表达式/CMakeLists.txt

set(SOURCE_FILES Expression.cpp NumericExpression.cpp)
add_library(experssion ${SOURCE_FILES})


target_link_libraries(experssion
PUBLIC
    ast
)
    
# get the include dirs 
target_include_directories(experssion 
INTERFACE  
    .
PUBLIC 
    ${LLVM_INCLUDE_DIRS}
    ${PROJECT_SOURCE_DIR}/contexts
    ${PROJECT_SOURCE_DIR}/ast 
    ${PROJECT_SOURCE_DIR}/meta

)

# INTERFACE tells to use includes to other libs but on the ast

前端/CMakeLists.txt


set(SOURCE_FILES Parser.cpp)

add_library(frontend STATIC ${SOURCE_FILES})

target_link_libraries(frontend 
PRIVATE 
    ast 
)

# get the include dirs 
target_include_directories(frontend 
INTERFACE 
    .
PRIVATE
    ${PROJECT_BINARY_DIR}
    ${PROJECT_SOURCE_DIR}/ast
    ${PROJECT_SOURCE_DIR}/contexts
    ${PROJECT_SOURCE_DIR}/ast/expression
)

元/CMakeLists.txt

add_library(meta INTERFACE)    
# get the include dirs 
target_include_directories(meta INTERFACE .)

这编译时没有错误,但在链接期间失败,并出现以下错误

[100%] Linking CXX executable slangLLVM
/usr/bin/ld: cannot find -lexpression
collect2: error: ld returned 1 exit status
make[3]: *** [callSlang/CMakeFiles/slangLLVM.dir/build.make:93: callSlang/slangLLVM] Error 1
make[3]: Leaving directory '/home/nithin/learn/c-cpp/slang-llvm/build'
make[2]: *** [CMakeFiles/Makefile2:392: callSlang/CMakeFiles/slangLLVM.dir/all] Error 2
make[2]: Leaving directory '/home/nithin/learn/c-cpp/slang-llvm/build'
make[1]: *** [Makefile:84: all] Error 2
make[1]: Leaving directory '/home/nithin/learn/c-cpp/slang-llvm/build'
make: *** [Makefile:4: all] Error 2

CallSlang 依赖于 ast,ast 依赖于表达式,反之亦然。

我已经尝试了链接中的解决方案,但到目前为止它对我不起作用。我真的是 CMake 的新手。是由 ast 和 expression 的循环调用引起的,如果是这样,如何纠正? 完整的代码可以在此链接中找到

有人可以告诉我可能的原因和解决方案吗?如果我遗漏了任何东西,过分强调或低估了某个特定点,请在评论中告诉我。提前非常感谢您抽出时间接受采访。

C++ CMake 生成文件 链接器 错误

评论

0赞 Nithin Varghese 8/18/2022
您在 ast/CMakeLists 中@273K建议.txt?如果是这样,我已经尝试过并遇到同样的错误。
2赞 vre 8/18/2022
您的库目标已调用,但您的项目链接到 。修正你的错别字,它应该可以工作。experssionexpression
0赞 Tsyvarev 8/18/2022
@273K:CMake完美地允许用于与尚未创建的目标的链接,因此和之间的顺序并不重要。只有第一个参数应该表示已经存在的目标。target_link_librariestarget_link_librariesadd_subdirectorytarget_link_libraries

答: 暂无答案