在gtest中,如何添加库文件到可执行的测试文件
in gtest, how to add lib file to executable test file
我总是得到undefined reference to m()
,这是我的代码:
ex.c
#include "stdio.h"
void m() {
}
ex.h
void m();
ex_test.cpp
#include "gtest/gtest.h"
#include "ex.h"
TEST(m, 1) {
m();
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(try)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(src ex.c)
add_executable(try ${src})
add_subdirectory(gtest)
include_directories(${gtest_SOURCE_DIR} ${gtest_SOURCE_DIR}/include)
add_executable(ex_test ex_test.cpp ex.c)
target_link_libraries(ex_test gtest gtest_main)
这是我的输出(对不起,sofraise "most code"错误时,我复制它
ex.c
编译为C。ex_test.cpp
编译为c++,但指的是m()
来自ex.c
,所以在ex_test.cpp
中您需要通知编译器ex.h
中的声明具有C链接(因此没有名称混淆)。
替换:
#include "ex.h"
:
extern "C" {
#include "ex.h"
}
您的问题标题正好指出了问题所在:您应该将ex
的库添加到ex_test
所需的库文件中。
编译ex.c
,然后将编译结果文件ex.a
附加到ex_test
所需的库文件中。你可以这样做,write CMakeLists.txt
文件如下:
add_library(ex ex.c)
target_link_libraries(ex_test ex gtest gtest_main)
相关文章:
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- CMake:如何将库 A 链接到库 B,然后将可执行文件链接到库 A
- 如何使字符串出现在编译的二进制可执行文件的开头?
- 程序在使用 system() 启动另一个可执行文件时停止
- 如何更改使用 CPack 安装的可执行文件的名称?
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- CMake:我们可以为一组不形成可执行文件或库的特定文件指定包含目录吗?
- 如何将图像传入C++可执行文件并将输出图像存储在新目录中?
- 共享库 (.so) 没有扩展名的 Linux 可执行文件之间的区别?
- 如何在 linux 可执行文件中使用静态库
- 错误:当我从"WinDbg"打开可执行文件时,找不到符号文件。默认导出 ntdll 的符号.dll
- 编译的C可执行文件被Windows defender检测为病毒
- Linux 可执行文件通过 dlopen 在emplace_back崩溃打开共享库
- 是否可以在命令行中将输入参数传递给可执行文件
- 如何在可执行文件中存储常量数组?
- 如果包含映射的静态库与可执行文件和动态库链接,静态映射(变量)是否会被多次释放?
- c ++:如何为每个特征制作特定的二进制文件(可执行文件)?
- 在英特尔C++编译器 (Linux) 的配置搜索路径中找不到文件可执行文件
- 在运行时动态更新二进制文件(可执行文件)而不停止的任何类型的方法