与CMAKE(MACOS)叮当的架构x86_64的未定义符号

发布于 2025-02-09 20:18:33 字数 9552 浏览 0 评论 0 原文

我是CMAKE和LLVM/CLANG的新手,因此我使用Brew安装了LLVM( BREW INSTALS LLVM ,并按照Homebrew export> export path =“/usr/usr/local/opt/llvm更新了我的路径/bin:$ path“ )用于学习目的,我在macos中尝试了一个简单的main.c :(

注意:我在 1 2 3 4 a href =“ https://stackoverflow.com/questions/25782537/cmake-and-clang-tooling-linking-linking-error-com-----------------25783251 #25783251”> 5 https://stackoverflow.com/questions/30867712/add-llvm-to-project-using-cmake"> 6 , 7 但到目前为止的答案对我没有用)

main.c

#include <clang-c/CXString.h>
#include <stdio.h>

void show_clang_version(void) {
        CXString version = clang_getClangVersion();
        printf("%s\n", clang_getCString(version));
        clang_disposeString(version);
}

int main( int argc, const char *const * argv )
{
        show_clang_version();
        return 0;
}

,然后是“简单”的cmakelists.txt

cmake_minimum_required(VERSION 3.0.0)
project(cmake-libclang VERSION 0.1.0)


set(triple "x86-64")
set(CMAKE_C_COMPILER clang)
set(CMAKE_LINKER ld64.lld)


message(STATUS "C   compiler: ${CMAKE_C_COMPILER_ID}  version: ${CMAKE_C_COMPILER_VERSION}" )
message(STATUS "C++ compiler: ${CMAKE_CXX_COMPILER_ID} version: ${CMAKE_CXX_COMPILER_VERSION}" )

######################################################################
# LLVM Package
######################################################################

find_package(LLVM REQUIRED CONFIG)

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "Using LLVM prefix directory: ${LLVM_PREFIX_DIR}")
message(STATUS "Using LLVM include directory: ${LLVM_INCLUDE_DIRS}")
message(STATUS "Using LLVM link directories: ${LLVM_LIBRARY_DIRS}")
message(STATUS "Using LLVM link directory: ${LLVM_LIBRARY_DIR}")
message(STATUS "Using LLVM binary directory: ${LLVM_BINARY_DIR}")
message(STATUS "Using LLVM definitions: ${LLVM_DEFINITIONS}")
message(STATUS "Using LLVM search paths: ${LLVM_SEARCH_PATHS}")

include_directories(${LLVM_INCLUDE_DIRS})
link_directories(${LLVM_LIBRARY_DIRS})
add_definitions(${LLVM_DEFINITIONS})

######################################################################
# CLANG package
######################################################################
find_package(CLANG CONFIG REQUIRED)
set(CLANG_LIBRARY_DIR ${LLVM_INCLUDE_DIRS})
set(CLANG_LIBRARY_DIRS ${CLANG_LIBRARY_DIR})

message(STATUS "Using ClangConfig.cmake in: ${CLANG_CMAKE_DIR}")
message(STATUS "Using CLANG include directory: ${CLANG_INCLUDE_DIRS}")
message(STATUS "Using CLANG link directory: ${CLANG_LIBRARY_DIR}")
message(STATUS "Using CLANG link directory: ${CLANG_LIBRARY_DIRS}")
message(STATUS "Using CLANG built-in directory: ${CLANG_BUILTIN_DIR}")
message(STATUS "Using CLANG_LIBRARY_DIRSNG definitions: ${CLANG_DEFINITIONS}")
message(STATUS "Using CLANG lib: ${CLANG_CLANG_LIB}")
message(STATUS "Using CLANG libs: ${LIBCLANG_LIBRARY}")

include_directories(${CLANG_INCLUDE_DIRS})
link_directories(${CLANG_LIBRARY_DIR})
add_definitions(${CLANG_DEFINITIONS})

######################################################################
# Parameters
######################################################################

add_executable(cmake-libclang main.c)

# Find the libraries that correspond to the LLVM components
# that we wish to use and link against LLVM libraries
llvm_map_components_to_libnames(llvm_libs -13)
target_link_libraries(cmake-libclang ${llvm_libs})

所以我以这种方式构建,一切似乎都可以:

$ cmake .
-- The C compiler identification is AppleClang 12.0.5.12050022
-- The CXX compiler identification is AppleClang 12.0.5.12050022
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- C   compiler: AppleClang  version: 12.0.5.12050022
-- C++ compiler: AppleClang version: 12.0.5.12050022
-- Found ZLIB: /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/lib/libz.tbd (found version "1.2.11")
-- Found LibXml2: /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/lib/libxml2.tbd (found version "2.9.4")
-- Found LLVM 13.0.1
-- Using LLVMConfig.cmake in: /usr/local/opt/llvm/lib/cmake/llvm
-- Using LLVM prefix directory:
-- Using LLVM include directory: /usr/local/opt/llvm/include
-- Using LLVM link directories: /usr/local/opt/llvm/lib
-- Using LLVM link directory: /usr/local/opt/llvm/lib
-- Using LLVM binary directory: /usr/local/opt/llvm
-- Using LLVM definitions: -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
-- Using LLVM search paths:
-- Using ClangConfig.cmake in: /usr/local/opt/llvm/lib/cmake/clang
-- Using CLANG include directory: /usr/local/opt/llvm/include
-- Using CLANG link directory: /usr/local/opt/llvm/include
-- Using CLANG link directory: /usr/local/opt/llvm/include
-- Using CLANG built-in directory:
-- Using CLANG definitions:
-- Using CLANG lib:
-- Using CLANG libs:
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/veronica/test-project

现在是怪异的部分。错误如下:

cmake --build . --clean-first
[ 50%] Building C object CMakeFiles/cmake-libclang.dir/main.c.o
/Users/veronica/main.c:5:28: warning: implicit declaration of function 'clang_getClangVersion' is invalid in C99 [-Wimplicit-function-declaration]
        CXString version = clang_getClangVersion();
                           ^
/Users/veronica/main.c:5:18: error: initializing 'CXString' with an expression of incompatible type 'int'
        CXString version = clang_getClangVersion();
                 ^         ~~~~~~~~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
make[2]: *** [CMakeFiles/cmake-libclang.dir/main.c.o] Error 1
make[1]: *** [CMakeFiles/cmake-libclang.dir/all] Error 2
make: *** [all] Error 2

我搜索函数:

grep clang_getClangVersion /usr/local/opt/llvm/include/clang-c/Index.h
CINDEX_LINKAGE CXString clang_getClangVersion(void);

,因此我将index.h包括在main.c.c中。

#include <clang-c/CXString.h>
#include <clang-c/Index.h>
...

,但最后我得到了这个错误:

$ cmake --build .
[ 50%] Building C object CMakeFiles/cmake-libclang.dir/main.c.o
[100%] Linking C executable cmake-libclang
Undefined symbols for architecture x86_64:
  "_clang_disposeString", referenced from:
      _show_clang_version in main.c.o
  "_clang_getCString", referenced from:
      _show_clang_version in main.c.o
  "_clang_getClangVersion", referenced from:
      _show_clang_version in main.c.o
ld: symbol(s) not found for architecture x86_64
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [cmake-libclang] Error 1
make[1]: *** [CMakeFiles/cmake-libclang.dir/all] Error 2
make: *** [all] Error 2

这是我的llvm config:

$ llvm-config --version --prefix --includedir --libdir --libs
13.0.1
/usr/local/Cellar/llvm/13.0.1_1
/usr/local/Cellar/llvm/13.0.1_1/include
/usr/local/Cellar/llvm/13.0.1_1/lib
-lLLVM-13

i会喜欢一个解释(或更好地解决此问题的建议),因为/usr/usr/local/opt/opt/llvm/lib/say“ mach-o 64 lib shared osss Library x86_64”下的所有.dylib文件说“ Mach-O 64位

”欢迎想法。

PS:这是使用-v和cmake使用-v的详细输出

[ 50%] Linking C executable cmake-libclang
/Users/veronica/vcpkg/downloads/tools/cmake-3.22.2-osx/cmake-3.22.2-macos-universal/CMake.app/Contents/bin/cmake -E cmake_link_script CMakeFiles/cmake-libclang.dir/link.txt --verbose=1
clang  -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk -mmacosx-version-min=11.6 -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/cmake-libclang.dir/main.c.o -o cmake-libclang   -L/usr/local/opt/llvm/lib  -L/usr/local/opt/llvm/include  -Wl,-rpath,/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/include -lLLVM-13
Undefined symbols for architecture x86_64:
  "_clang_disposeString", referenced from:
      _show_clang_version in main.c.o
  "_clang_getCString", referenced from:
      _show_clang_version in main.c.o
  "_clang_getClangVersion", referenced from:
      _show_clang_version in main.c.o
ld: symbol(s) not found for architecture x86_64
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [cmake-libclang] Error 1
make[1]: *** [CMakeFiles/cmake-libclang.dir/all] Error 2
make: *** [all] Error 2

I am new to CMake and LLVM/Clang, so I installed LLVM with brew (brew install llvm and updated my PATH as suggested by homebrew export PATH="/usr/local/opt/llvm/bin:$PATH") for learning purposes and I tried a simple main.c in my macOS:

(Note: I researched for similar answers to this particular issue in 1, 2, 3, 4, 5, 6, 7 and 8 but answers so far were not useful for me)

main.c

#include <clang-c/CXString.h>
#include <stdio.h>

void show_clang_version(void) {
        CXString version = clang_getClangVersion();
        printf("%s\n", clang_getCString(version));
        clang_disposeString(version);
}

int main( int argc, const char *const * argv )
{
        show_clang_version();
        return 0;
}

And then a "simple" CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(cmake-libclang VERSION 0.1.0)


set(triple "x86-64")
set(CMAKE_C_COMPILER clang)
set(CMAKE_LINKER ld64.lld)


message(STATUS "C   compiler: ${CMAKE_C_COMPILER_ID}  version: ${CMAKE_C_COMPILER_VERSION}" )
message(STATUS "C++ compiler: ${CMAKE_CXX_COMPILER_ID} version: ${CMAKE_CXX_COMPILER_VERSION}" )

######################################################################
# LLVM Package
######################################################################

find_package(LLVM REQUIRED CONFIG)

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "Using LLVM prefix directory: ${LLVM_PREFIX_DIR}")
message(STATUS "Using LLVM include directory: ${LLVM_INCLUDE_DIRS}")
message(STATUS "Using LLVM link directories: ${LLVM_LIBRARY_DIRS}")
message(STATUS "Using LLVM link directory: ${LLVM_LIBRARY_DIR}")
message(STATUS "Using LLVM binary directory: ${LLVM_BINARY_DIR}")
message(STATUS "Using LLVM definitions: ${LLVM_DEFINITIONS}")
message(STATUS "Using LLVM search paths: ${LLVM_SEARCH_PATHS}")

include_directories(${LLVM_INCLUDE_DIRS})
link_directories(${LLVM_LIBRARY_DIRS})
add_definitions(${LLVM_DEFINITIONS})

######################################################################
# CLANG package
######################################################################
find_package(CLANG CONFIG REQUIRED)
set(CLANG_LIBRARY_DIR ${LLVM_INCLUDE_DIRS})
set(CLANG_LIBRARY_DIRS ${CLANG_LIBRARY_DIR})

message(STATUS "Using ClangConfig.cmake in: ${CLANG_CMAKE_DIR}")
message(STATUS "Using CLANG include directory: ${CLANG_INCLUDE_DIRS}")
message(STATUS "Using CLANG link directory: ${CLANG_LIBRARY_DIR}")
message(STATUS "Using CLANG link directory: ${CLANG_LIBRARY_DIRS}")
message(STATUS "Using CLANG built-in directory: ${CLANG_BUILTIN_DIR}")
message(STATUS "Using CLANG_LIBRARY_DIRSNG definitions: ${CLANG_DEFINITIONS}")
message(STATUS "Using CLANG lib: ${CLANG_CLANG_LIB}")
message(STATUS "Using CLANG libs: ${LIBCLANG_LIBRARY}")

include_directories(${CLANG_INCLUDE_DIRS})
link_directories(${CLANG_LIBRARY_DIR})
add_definitions(${CLANG_DEFINITIONS})

######################################################################
# Parameters
######################################################################

add_executable(cmake-libclang main.c)

# Find the libraries that correspond to the LLVM components
# that we wish to use and link against LLVM libraries
llvm_map_components_to_libnames(llvm_libs -13)
target_link_libraries(cmake-libclang ${llvm_libs})

So I build this way and everything seems ok:

$ cmake .
-- The C compiler identification is AppleClang 12.0.5.12050022
-- The CXX compiler identification is AppleClang 12.0.5.12050022
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- C   compiler: AppleClang  version: 12.0.5.12050022
-- C++ compiler: AppleClang version: 12.0.5.12050022
-- Found ZLIB: /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/lib/libz.tbd (found version "1.2.11")
-- Found LibXml2: /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/lib/libxml2.tbd (found version "2.9.4")
-- Found LLVM 13.0.1
-- Using LLVMConfig.cmake in: /usr/local/opt/llvm/lib/cmake/llvm
-- Using LLVM prefix directory:
-- Using LLVM include directory: /usr/local/opt/llvm/include
-- Using LLVM link directories: /usr/local/opt/llvm/lib
-- Using LLVM link directory: /usr/local/opt/llvm/lib
-- Using LLVM binary directory: /usr/local/opt/llvm
-- Using LLVM definitions: -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
-- Using LLVM search paths:
-- Using ClangConfig.cmake in: /usr/local/opt/llvm/lib/cmake/clang
-- Using CLANG include directory: /usr/local/opt/llvm/include
-- Using CLANG link directory: /usr/local/opt/llvm/include
-- Using CLANG link directory: /usr/local/opt/llvm/include
-- Using CLANG built-in directory:
-- Using CLANG definitions:
-- Using CLANG lib:
-- Using CLANG libs:
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/veronica/test-project

Now here comes the weird part. The error is as follows:

cmake --build . --clean-first
[ 50%] Building C object CMakeFiles/cmake-libclang.dir/main.c.o
/Users/veronica/main.c:5:28: warning: implicit declaration of function 'clang_getClangVersion' is invalid in C99 [-Wimplicit-function-declaration]
        CXString version = clang_getClangVersion();
                           ^
/Users/veronica/main.c:5:18: error: initializing 'CXString' with an expression of incompatible type 'int'
        CXString version = clang_getClangVersion();
                 ^         ~~~~~~~~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
make[2]: *** [CMakeFiles/cmake-libclang.dir/main.c.o] Error 1
make[1]: *** [CMakeFiles/cmake-libclang.dir/all] Error 2
make: *** [all] Error 2

I search the function:

grep clang_getClangVersion /usr/local/opt/llvm/include/clang-c/Index.h
CINDEX_LINKAGE CXString clang_getClangVersion(void);

So I include the Index.h in main.c

#include <clang-c/CXString.h>
#include <clang-c/Index.h>
...

But finally I get this error:

$ cmake --build .
[ 50%] Building C object CMakeFiles/cmake-libclang.dir/main.c.o
[100%] Linking C executable cmake-libclang
Undefined symbols for architecture x86_64:
  "_clang_disposeString", referenced from:
      _show_clang_version in main.c.o
  "_clang_getCString", referenced from:
      _show_clang_version in main.c.o
  "_clang_getClangVersion", referenced from:
      _show_clang_version in main.c.o
ld: symbol(s) not found for architecture x86_64
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [cmake-libclang] Error 1
make[1]: *** [CMakeFiles/cmake-libclang.dir/all] Error 2
make: *** [all] Error 2

This is my LLVM config:

$ llvm-config --version --prefix --includedir --libdir --libs
13.0.1
/usr/local/Cellar/llvm/13.0.1_1
/usr/local/Cellar/llvm/13.0.1_1/include
/usr/local/Cellar/llvm/13.0.1_1/lib
-lLLVM-13

I would love an explanation (or better suggestions on how to fix this) to what's happening here because all .dylib files under /usr/local/opt/llvm/lib/ says "Mach-O 64-bit dynamically linked shared library x86_64"

Any ideas are welcomed.

PS: Here is the verbose output using -v with cmake

[ 50%] Linking C executable cmake-libclang
/Users/veronica/vcpkg/downloads/tools/cmake-3.22.2-osx/cmake-3.22.2-macos-universal/CMake.app/Contents/bin/cmake -E cmake_link_script CMakeFiles/cmake-libclang.dir/link.txt --verbose=1
clang  -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk -mmacosx-version-min=11.6 -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/cmake-libclang.dir/main.c.o -o cmake-libclang   -L/usr/local/opt/llvm/lib  -L/usr/local/opt/llvm/include  -Wl,-rpath,/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/include -lLLVM-13
Undefined symbols for architecture x86_64:
  "_clang_disposeString", referenced from:
      _show_clang_version in main.c.o
  "_clang_getCString", referenced from:
      _show_clang_version in main.c.o
  "_clang_getClangVersion", referenced from:
      _show_clang_version in main.c.o
ld: symbol(s) not found for architecture x86_64
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [cmake-libclang] Error 1
make[1]: *** [CMakeFiles/cmake-libclang.dir/all] Error 2
make: *** [all] Error 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文