gpt4 book ai didi

CMake 无法正确链接库

转载 作者:行者123 更新时间:2023-11-30 16:33:36 27 4
gpt4 key购买 nike

我正在做作业,我的项目树是

.
├── CMakeLists.txt
├── main.c
├── src
│   ├── CMakeLists.txt
│   ├── game
│   │   ├── game.c
│   │   └── game.h
│   └── main.c
└── test
├── CMakeLists.txt
├── homework_tests
│   ├── CMakeLists.txt
│   ├── gtest.cpp
│   └── gtest.h
└── lib
└──googletest
└──googletest
└──[library files (not compiled)]

main.c 文件中只有一个“Hello, world”。在阅读之前:我正在尝试:
1. 从源文件构建目标src.so
2. 构建 google 测试库(在 test/lib/ 文件夹中)
3. 从 tests/ 文件夹
构建目标runBasicTests4. 将它们链接在一起

我承认我不是 CMake 高手,所以请注意这一点。

文件

现在,假设我们位于项目根目录中。

./:

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(c_homework)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -std=c11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES
main.c)

add_executable(homework_run ${SOURCE_FILES})

add_subdirectory(src)
add_subdirectory(test)

target_link_libraries(homework_run src.so)

src/:

CMakeLists.txt

set(SOURCE_FILES
game/game.c)

add_library(src.so ${SOURCE_FILES})

target_include_directories (src.so PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

游戏/game.h

#ifndef C_HOMEWORK_GAME_H
#define C_HOMEWORK_GAME_H

#include <stdbool.h>

struct game_t {
int id, price;
const char *country_code;
};

bool is_game_available(struct game_t, const char[4]);
int how_many_available(struct game_t*, size_t, const char[4]);

#endif //C_HOMEWORK_GAME_H

游戏/game.c

#include <string.h>
#include "game.h"

bool is_game_available(struct game_t game, const char country_code[4])
{
return strcmp(game.country_code, country_code) == 0;
}

int how_many_available(struct game_t * games, size_t game_count, const
char country_code[4]) {
int available_games = 0;
for (int i = 0; i < game_count; i++) {
if (is_game_available(games[i], country_code)) {
available_games++;
}
}
return available_games;
}

测试/:

CMakeLists.txt

project(homework_tests)

add_subdirectory(lib/googletest/googletest)
add_subdirectory(homework_tests)

homework_tests/CMakeLists.txt

include_directories(${gTest_SOURCE_DIR}/include ${gTest_SOURCE_DIR})

add_executable(runBasicTests
gtest.cpp)

target_link_libraries(runBasicTests gtest gtest_main)
target_link_libraries(runBasicTests src.so)

homework_tests/gtest.cpp

#include "gtest.h"
#include <game/game.h>

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

TEST(gameTests, gameTest1) {
struct game_t game{};
game.country_code = "359";
game.id = 1;
game.price = 69;
EXPECT_EQ(is_game_available(game, "359"), true);
}

homework_tests/gtest.h

#ifndef MYPROJECTNAME_GTEST_H
#define MYPROJECTNAME_GTEST_H

#include "gtest/gtest.h"

#endif //MYPROJECTNAME_GTEST_H

问题是,当我尝试运行测试时,我收到链接器错误:

/opt/clion-2018.1/bin/cmake/bin/cmake --build 
/home/denis/CLionProjects/c_homework/cmake-build-debug --target
runBasicTests -- -j 1

[ 25%] Built target gtest
Scanning dependencies of target src.so
[ 37%] Building C object src/CMakeFiles/src.so.dir/game/game.c.o
[ 50%] Linking C static library libsrc.so.a
[ 50%] **Built target src.so**
[ 75%] Built target gtest_main
[ 87%] Linking CXX executable runBasicTests
CMakeFiles/runBasicTests.dir/gtest.cpp.o: In function
`gameTests_gameTest1_Test::TestBody()':
/home/denis/CLionProjects/c_homework/test/homework_tests/gtest.cpp:18:

<--------------- Here ----------------->
undefined reference to `is_game_available(game_t, char const*)'
collect2: error: ld returned 1 exit status

test/homework_tests/CMakeFiles/runBasicTests.dir/build.make:98: recipe
for target 'test/homework_tests/runBasicTests' failed
make[3]: *** [test/homework_tests/runBasicTests] Error 1
CMakeFiles/Makefile2:294: recipe for target
'test/homework_tests/CMakeFiles/runBasicTests.dir/all' failed
make[2]: *** [test/homework_tests/CMakeFiles/runBasicTests.dir/all]
Error 2
CMakeFiles/Makefile2:306: recipe for target
'test/homework_tests/CMakeFiles/runBasicTests.dir/rule' failed
make[1]: *** [test/homework_tests/CMakeFiles/runBasicTests.dir/rule]
Error 2
Makefile:170: recipe for target 'runBasicTests' failed
make: *** [runBasicTests] Error 2
  • 不需要我的库的测试也能正确运行
  • 位于 src/ 文件夹中并与 src.so 库链接的目标也运行正确

最佳答案

问题是我从 C++ 代码调用 C 代码,而 C++ 编译器正在更改函数的名称(名称修改),这就是为什么我收到 undefined reference 错误。

解决方案是告诉编译器以下函数声明是外部“C”代码。

测试/homework_tests/gtest

#ifdef __cplusplus
extern "C" {
#endif
/* Declarations of this file */
bool is_game_available(struct game_t, const char[4]);
int how_many_available(struct game_t *, size_t, const char[4]);
#ifdef __cplusplus
}
#endif

问题就这样解决了。谢谢大家。 XD

关于CMake 无法正确链接库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49692773/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com