C++ 混合C++;和C/C++;基于CMake的静态库

C++ 混合C++;和C/C++;基于CMake的静态库,c++,gcc,cmake,g++,c99,C++,Gcc,Cmake,G++,C99,我已经搜索了各种论坛,找不到解决我问题的方法 我试图在C文件中编写函数的单元测试。我正在使用谷歌测试库。 虽然我遵循了指南,但项目并没有正确编译 用C++编写的测试在C文件中看不到函数。(未定义的引用) 下面我附上我的文件,也许有人会注意到我犯了一个错误。我没有主意了。 pir_driver.h #ifndef PIR_DRIVER_H_ #define PIR_DRIVER_H_ #ifdef __cplusplus extern "C" { #endif #incl

我已经搜索了各种论坛,找不到解决我问题的方法
我试图在C文件中编写函数的单元测试。我正在使用谷歌测试库。
虽然我遵循了指南,但项目并没有正确编译
用C++编写的测试在C文件中看不到函数。(未定义的引用)
下面我附上我的文件,也许有人会注意到我犯了一个错误。我没有主意了。

pir_driver.h

#ifndef PIR_DRIVER_H_
#define PIR_DRIVER_H_

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdint.h>

uint32_t xTaskGetTickCount();


#ifdef __cplusplus
} /* extern "C" */
#endif
pir_测试。cpp

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

TEST(PIR_Timer_Test, Start)
{
    uint32_t test =  xTaskGetTickCount() * 2;
    EXPECT_EQ(test, test);

}
test CMakeLists.txt

cmake_minimum_required(VERSION 3.8)
set(This pir_driver)
project(${This} C CXX)
include(Dart)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
enable_testing()
add_subdirectory(googletest)
set(Headers
    pir_driver.h
)
set(Source
    pir_driver.c
)
add_library(${This} STATIC ${Sources} ${Headers})
set_target_properties(${This} PROPERTIES LINKER_LANGUAGE C)
add_subdirectory(test)
cmake_minimum_required(VERSION 3.8)
set(This pir_test)
set(Sources pir_test.cpp )

add_executable(${This} ${Sources})
set_target_properties(${This} PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(${This} PUBLIC pir_driver gtest_main )

add_test( NAME ${This} COMMAND ${This})
编译日志:

[proc] Wykonywanie polecenia: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/xxx/ansi_c_projects/pir_test/build --config Debug --target all -- -j 10
[build] [ 14%] Linking C static library libpir_driver.a
[build] [ 28%] Building CXX object googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj
[build] [ 28%] Built target pir_driver
[build] [ 42%] Linking CXX static library ..\lib\libgtestd.a
[build] [ 42%] Built target gtest
[build] [ 57%] Building CXX object googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.obj
[build] [ 71%] Linking CXX static library ..\lib\libgtest_maind.a
[build] [ 71%] Built target gtest_main
[build] [ 85%] Building CXX object test/CMakeFiles/pir_test.dir/pir_test.cpp.obj
[build] [100%] Linking CXX executable pir_test.exe
[build] CMakeFiles\pir_test.dir/objects.a(pir_test.cpp.obj): In function `PIR_Timer_Test_Start_Test::TestBody()':
[build] C:/Users/xxx/ansi_c_projects/pir_test/test/pir_test.cpp:13: undefined reference to `xTaskGetTickCount'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [test\CMakeFiles\pir_test.dir\build.make:108: test/pir_test.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:1004: test/CMakeFiles/pir_test.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:113: all] Error 2

它不起作用的原因是您在主
CMakeLists.txt
中有一个输入错误:您定义变量
Source
,但在填充
pir\u驱动程序的源时使用变量
Sources
。因此,不会编译.c文件,链接器无法找到其中定义的符号

缺少的.c文件也是您首先需要手动设置链接器语言的原因。添加源文件后,可以删除
set\u target\u属性(${This}properties LINKER\u LANGUAGE C)
行,因为CMake将根据源文件的扩展名自行确定

为了避免将来出现此类问题,您可以使用

add_library(pir_driver STATIC)
target_sources(pir_driver
  PRIVATE
    pir_driver.h
    pir_driver.c
)

在CMake 3.11及更高版本中,不使用CMake变量来收集源和头。

外部“C”
块中放置系统头的
#include
指令可能不是一个好主意。只是说,非常感谢,现在我看到了使用变量的风险。我刚开始了解克马克。仍然缺少Target_include_目录,但错误是明确的。
add_library(pir_driver STATIC)
target_sources(pir_driver
  PRIVATE
    pir_driver.h
    pir_driver.c
)