Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CMake无法正确链接库_C_Cmake_Linker_Googletest - Fatal编程技术网

CMake无法正确链接库

CMake无法正确链接库,c,cmake,linker,googletest,C,Cmake,Linker,Googletest,我正在做作业,我的项目树是 . ├── CMakeLists.txt ├── main.c ├── src │   ├── CMakeLists.txt │   ├── game │   │   ├── game.c │   │   └── game.h │   └── main.c └── test ├── CMakeLists.txt ├── homework_tests │   ├── CMakeLists.txt │   ├── gtest.cpp

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

.
├── 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)]
主.c文件中只有一个“Hello,world”。 在阅读之前: 我正在努力:
1.从源文件构建目标
src.so

2.构建google测试库(在
test/lib/
文件夹中)
3.从
tests/
文件夹中构建目标
runBasicTests
4.把它们联系在一起

我承认我不是CMake大师,所以请注意这一点

文件夹 现在,假设我们在项目根

/
: CMakeLists.txt
src/
: CMakeLists.txt game/game.h 家庭作业测试/CMakeLists.txt 家庭作业测试/gtest.cpp 问题是,当我尝试运行测试时,我得到一个链接器错误:

/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
/opt/clion-2018.1/bin/cmake/bin/cmake--build
/home/denis/CLionProjects/c_作业/cmake构建调试——目标
runBasicTests--j 1
[25%]构建目标gtest
扫描目标src.so的依赖项
[37%]构建C对象src/CMakeFiles/src.so.dir/game/game.C.o
[50%]链接C静态库libsrc.so.a
[50%]**构建目标src.so**
[75%]已建目标gtest_main
[87%]链接CXX可执行runBasicTests
CMakeFiles/runBasicTests.dir/gtest.cpp.o:函数中
`gameTests\u gameTest1\u Test::TestBody()':
/home/denis/CLionProjects/c_家庭作业/测试/家庭作业测试/gtest.cpp:18:
未定义的对“是否有游戏可用(游戏,字符常量*)”的引用
collect2:错误:ld返回了1个退出状态
测试/家庭作业\u测试/CMakeFiles/runBasicTests.dir/build.make:98:recipe
对于目标“测试/家庭作业测试/运行基本测试”失败
生成[3]:***[测试/家庭作业\u测试/运行基本测试]错误1
CMakeFiles/Makefile2:294:目标的配方
“测试/家庭作业\u测试/CMakeFiles/runBasicTests.dir/all”失败
make[2]:***[测试/家庭作业\u测试/CMakeFiles/runBasicTests.dir/all]
错误2
CMakeFiles/Makefile2:306:目标的配方
“测试/家庭作业\u测试/CMakeFiles/runBasicTests.dir/rule”失败
make[1]:***[测试/家庭作业\u测试/CMakeFiles/runBasicTests.dir/rule]
错误2
Makefile:170:目标“runBasicTests”的配方失败
make:**[runBasicTests]错误2
  • 不需要我的库的测试可以正常运行
  • 位于
    src/
    文件夹中并与
    src链接的目标。因此
    库也会正确运行

  • 问题是我从C++代码中调用C代码,C++编译器正在改变函数的名称(名字的变形),这就是为什么我得到和<代码>未定义的引用< /代码>错误。 解决方案是告诉编译器以下函数声明是外部“C”代码

    测试/家庭作业\u测试/gtest
    这就解决了问题。谢谢大家。XD

    这可能与以下事实有关:源代码是用
    C
    编写的,而
    C++
    中的测试可能不会重复。与这一事实有关。你的<代码>游戏。H//Cuth.Head文件需要找出它是由C或C++编译器编译的(提示:< C++ >
    set(SOURCE_FILES
            game/game.c)
    
    add_library(src.so ${SOURCE_FILES})
    
    target_include_directories (src.so PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
    
    #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
    
    #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;
    }
    
    project(homework_tests)
    
    add_subdirectory(lib/googletest/googletest)
    add_subdirectory(homework_tests)
    
    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)
    
    #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);
    }
    
    #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
    
    #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