如何通过cmake将libgit2包含在源代码核心中

fgw7neuy  于 2022-12-04  发布在  Git
关注(0)|答案(1)|浏览(194)

我想将libgit 2作为源代码导入我的演示中
我使用macOS Monterey与苹果M1和Jetbrains Clion
这是我的dir结构

demo
  |--CMakeLists.txt
  |--libgit2(from git clone with v1.5.0)
  |--main.c

CMakeLists.txt

cmake_minimum_required(VERSION 3.23)
project(libgit2_demo C)
set(CMAKE_C_STANDARD 99)

set(CMAKE_PREFIX_PATH /opt/homebrew)
set(CMAKE_PREFIX_PATH /opt/homebrew/opt/libssh2)
set(CMAKE_PREFIX_PATH /opt/homebrew/opt/openssl@3)
set(BUILD_EXAMPLES OFF)
set(BUILD_TESTS OFF)
set(BUILD_SHARED_LIBS OFF)
set(USE_SSH ON)
set(CMAKE_OSX_ARCHITECTURES arm64)
set(CMAKE_BUILD_TYPE Debug)

find_package(PkgConfig REQUIRED)
pkg_check_modules(SSH2 REQUIRED IMPORTED_TARGET libssh2 openssl libssl libcrypto zlib libpcre)

# I known libgit2's denpendent from https://libgit2.org/docs/guides/build-and-link/
find_library(CF CoreFoundation)
find_library(SEC Security)
find_library(ICONV iconv)
find_library(SYS System)

add_subdirectory(${CMAKE_SOURCE_DIR}/libgit2)
add_executable(libgit2_demo main.c)

target_include_directories(libgit2_demo PUBLIC libgit2/include)
target_link_libraries(
        libgit2_demo PUBLIC libgit2
        PkgConfig::SSH2
        ${CF} ${SEC} ${ICONV} ${SYS}
)

我使用此命令准备cmake

mkdir build
cd build
cmake .. \
    -DCMAKE_BUILD_TYPE=Debug \
    -DCMAKE_OSX_ARCHITECTURES='arm64' \
    -DBUILD_SHARED_LIBS=OFF \
    -DUSE_SSH=ON \
    -DBUILD_TESTS=OFF \
    -DCMAKE_PREFIX_PATH='/opt/homebrew/opt/libssh2;/opt/homebrew/opt/openssl@3'

目前为止一切顺利。

-- Enabled features:
 * nanoseconds, support nanosecond precision file mtimes and ctimes
 * HTTPS, using SecureTransport
 * SHA1, using CollisionDetection
 * SHA256, using CommonCrypto
 * http-parser, http-parser support (bundled)
 * regex, using system regcomp_l
 * SSH, SSH transport support
 * zlib, using system zlib
 * futimens, futimens support
 * threadsafe, threadsafe support
 * ntlmclient, NTLM authentication support for Unix
 * iconv, iconv encoding conversion support

-- Disabled features:
 * debugpool, debug pool allocator
 * debugalloc, debug strict allocators
 * debugopen, path validation in open
 * SPNEGO, SPNEGO authentication support

-- Configuring done
-- Generating done
-- Build files have been written to: /Users/bppleman/CLionProjects/libgit2-demo/build

然后我用命令造:

cmake --build . --target libgit2_demo

但收到链接错误

[  0%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/annotated_commit.c.o
[  1%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/apply.c.o
[  1%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/attr.c.o
[  2%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/attr_file.c.o
[  4%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/attrcache.c.o
[  4%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/blame.c.o
[  5%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/blame_git.c.o
[  5%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/blob.c.o
[  6%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/branch.c.o
[  6%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/buf.c.o
[  8%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/cache.c.o
[  8%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/checkout.c.o
[  9%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/cherrypick.c.o
[  9%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/clone.c.o
[ 10%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/commit.c.o
[ 10%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/commit_graph.c.o
[ 12%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/commit_list.c.o
[ 12%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/config.c.o
[ 13%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/config_cache.c.o
[ 15%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/config_entries.c.o
[ 15%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/config_file.c.o
[ 16%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/config_mem.c.o
[ 16%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/config_parse.c.o
[ 17%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/config_snapshot.c.o
[ 17%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/crlf.c.o
[ 19%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/delta.c.o
[ 19%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/describe.c.o
[ 20%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/diff.c.o
[ 20%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/diff_driver.c.o
[ 21%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/diff_file.c.o
[ 21%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/diff_generate.c.o
[ 23%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/diff_parse.c.o
[ 23%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/diff_print.c.o
[ 24%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/diff_stats.c.o
[ 26%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/diff_tform.c.o
[ 26%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/diff_xdiff.c.o
[ 27%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/email.c.o
[ 27%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/errors.c.o
[ 28%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/fetch.c.o
[ 28%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/fetchhead.c.o
[ 30%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/filter.c.o
[ 30%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/graph.c.o
[ 31%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/hashsig.c.o
[ 31%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/ident.c.o
[ 32%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/idxmap.c.o
[ 32%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/ignore.c.o
[ 34%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/index.c.o
[ 34%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/indexer.c.o
[ 35%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/iterator.c.o
[ 35%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/libgit2.c.o
[ 36%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/mailmap.c.o
[ 38%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/merge.c.o
[ 38%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/merge_driver.c.o
[ 39%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/merge_file.c.o
[ 39%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/message.c.o
[ 41%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/midx.c.o
[ 41%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/mwindow.c.o
[ 42%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/netops.c.o
[ 42%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/notes.c.o
[ 43%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/object.c.o
[ 43%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/object_api.c.o
[ 45%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/odb.c.o
[ 45%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/odb_loose.c.o
[ 46%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/odb_mempack.c.o
[ 46%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/odb_pack.c.o
[ 47%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/offmap.c.o
[ 49%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/oid.c.o
[ 49%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/oidarray.c.o
[ 50%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/oidmap.c.o
[ 50%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/pack-objects.c.o
[ 52%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/pack.c.o
[ 52%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/parse.c.o
[ 53%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/patch.c.o
[ 53%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/patch_generate.c.o
[ 54%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/patch_parse.c.o
[ 54%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/path.c.o
[ 56%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/pathspec.c.o
[ 56%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/proxy.c.o
[ 57%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/push.c.o
[ 57%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/reader.c.o
[ 58%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/rebase.c.o
[ 60%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/refdb.c.o
[ 60%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/refdb_fs.c.o
[ 61%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/reflog.c.o
[ 61%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/refs.c.o
[ 63%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/refspec.c.o
[ 63%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/remote.c.o
[ 64%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/repository.c.o
[ 64%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/reset.c.o
[ 65%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/revert.c.o
[ 65%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/revparse.c.o
[ 67%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/revwalk.c.o
[ 67%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/signature.c.o
[ 68%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/stash.c.o
[ 68%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/status.c.o
[ 69%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/strarray.c.o
[ 69%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/streams/mbedtls.c.o
[ 71%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/streams/openssl.c.o
[ 72%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/streams/openssl_dynamic.c.o
[ 72%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/streams/openssl_legacy.c.o
[ 73%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/streams/registry.c.o
[ 73%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/streams/socket.c.o
[ 75%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/streams/stransport.c.o
[ 75%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/streams/tls.c.o
[ 76%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/submodule.c.o
[ 76%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/sysdir.c.o
[ 78%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/tag.c.o
[ 78%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/threadstate.c.o
[ 79%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/trace.c.o
[ 79%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/trailer.c.o
[ 80%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transaction.c.o
[ 80%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transport.c.o
[ 82%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/auth.c.o
[ 83%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/auth_negotiate.c.o
[ 83%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/auth_ntlm.c.o
[ 84%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/credential.c.o
[ 84%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/credential_helpers.c.o
[ 86%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/git.c.o
[ 86%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/http.c.o
[ 87%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/httpclient.c.o
[ 87%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/local.c.o
[ 89%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/smart.c.o
[ 89%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/smart_pkt.c.o
[ 90%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/smart_protocol.c.o
[ 90%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/ssh.c.o
[ 91%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/transports/winhttp.c.o
[ 91%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/tree-cache.c.o
[ 93%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/tree.c.o
[ 94%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/worktree.c.o
[ 94%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/xdiff/xdiffi.c.o
[ 95%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/xdiff/xemit.c.o
[ 95%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/xdiff/xhistogram.c.o
[ 97%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/xdiff/xmerge.c.o
[ 97%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/xdiff/xpatience.c.o
[ 98%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/xdiff/xprepare.c.o
[ 98%] Building C object libgit2/src/libgit2/CMakeFiles/libgit2.dir/xdiff/xutils.c.o
[ 98%] Built target libgit2
[100%] Building C object CMakeFiles/libgit2_demo.dir/main.c.o
[100%] Linking C executable libgit2_demo
Undefined symbols for architecture arm64:
  "_git__allocator", referenced from:
      _annotated_commit_init in annotated_commit.c.o
      _git_annotated_commit_from_ref in annotated_commit.c.o
      _git_annotated_commit_from_fetchhead in annotated_commit.c.o
      _git_annotated_commit_free in annotated_commit.c.o
      _git_apply__patch in apply.c.o
      _apply_one in apply.c.o
      _git_attr_get_many_with_session in attr.c.o
      ...
  "_git__getenv", referenced from:
      _http_proxy_env in remote.c.o
      __git_repository_open_ext_from_env in repository.c.o
      _git_sysdir_guess_global_dirs in sysdir.c.o
      _git_sysdir_guess_xdg_dirs in sysdir.c.o
  "_git__linenlen", referenced from:
      _git_parse_ctx_init in parse.c.o
      _git_parse_advance_line in parse.c.o
  "_git__memmem", referenced from:
      _packed_set_peeling_mode in refdb_fs.c.o
      _tag_parse in tag.c.o
  "_git__online_cpus", referenced from:
      _ll_find_deltas in pack-objects.c.o

// There are more link errors, I did not copy over

我的其他尝试:
在libgit 2-v1.3.0中,此cmake配置可以正确运行

xlpyo6sf

xlpyo6sf1#

这不是libgit2的问题,但我忘记链接util目标
公正
CMakeLists.txt

……
target_link_libraries(
-        lg2_demo PUBLIC libgit2
+        lg2_demo PUBLIC libgit2 util http-parser libgit2package
        PkgConfig::SSH2
        ${CF} ${SEC} ${ICONV} ${SYS}
)
……

libgit2项目中有多个目标必须同时链接到演示可执行文件

相关问题