链接C和C 文件
Linkage C and C++ files
我想和我构建的两个测试一起编译node.c,list.c和helper-functions.c。
我确定我没有尽我所能编写makefile。您可以检查一下吗?
CFLAGS= -Wall -Wvla -Werror
CXXFLAGS= -lgtest -lgtest_main -pthread
all:
mkdir -p build
gcc -c node.c $(CFLAGS) -o build/node.o
gcc -c list.c $(CFLAGS) -o build/list.o
gcc -c tests/helper-functions.c -o build/helper-functions.o
g++ tests/node-tests.cpp $(CXXFLAGS) -o build/node-tests.o
g++ tests/list-tests.cpp $(CXXFLAGS) -o build/list-tests.o
g++ build/list.o build/node.o build/helper-functions.o build/node-tests.o build/list-tests.o -o build/tests.out
./build/tests.out
当我运行make
时,输出为:
mkdir -p build
gcc -c node.c -Wall -Wvla -Werror -o build/node.o
gcc -c list.c -Wall -Wvla -Werror -o build/list.o
gcc -c tests/helper-functions.c -o build/helper-functions.o
g++ tests/node-tests.cpp -lgtest -lgtest_main -pthread -o build/node-tests.o
/tmp/ccFTgPVe.o: In function `CreateNode_FirstTest_Test::TestBody()':
node-tests.cpp:(.text+0x21): undefined reference to `createNode'
node-tests.cpp:(.text+0x31): undefined reference to `data'
node-tests.cpp:(.text+0x108): undefined reference to `next'
node-tests.cpp:(.text+0x1db): undefined reference to `freeNode'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
肯定会引用Node-Test.cpp中的所有这些功能。
node-tests.cpp:
extern "C"
{
#include "../../../../headers/list.h"
#include "../../../../headers/node.h"
#include "helper-functions.h"
#include <assert.h>
}
#include "gtest/gtest.h"
#include <iostream>
#include <cstdlib>
#include <string>
// Node* CreateNode(void* data,const size_t dataLength,void* deepCopy(void*,const size_t));
TEST(CreateNode, FirstTest) {
int x=10;
Node* pos=createNode(&x,sizeof(x));
EXPECT_TRUE(*(int*)data(pos)==x);
EXPECT_TRUE(next(pos)==NULL);
freeNode(pos);
}
list-tests.cpp类似于node-tests.cpp。
CFLAGS= -Wall -Wvla -Werror
CXXFLAGS= -lgtest -lgtest_main -pthread
all:
mkdir -p build
gcc -c node.c $(CFLAGS) -o build/node.o
gcc -c list.c $(CFLAGS) -o build/list.o
gcc -c tests/helper-functions.c $(CFLAGS) -o build/helper-functions.o
g++ -c tests/node-tests.cpp -o build/node-tests.o //added -c, removed $(CXXFLAGS)
g++ -c tests/list-tests.cpp -o build/list-tests.o // added -c, $(CXXFLAGS)
g++ build/list.o build/node.o build/helper-functions.o build/node-tests.o build/list-tests.o $(CXXFLAGS) -o build/tests.out // added $(CXXFLAGS)
./build/tests.out
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 无法将 GLFW 库与 Ubuntu 18.04 上的头文件链接
- 错误 VSP1011:无法获取调试信息.与 /配置文件链接器开关链接
- 将我的主输出库与测试可执行文件链接时出现问题
- CMake + Qt + GTest 文件链接
- 生成文件链接
- 使用LTO静态链接的可执行文件(链接时间优化):如何使用以前构建的库进行制作
- 生成文件链接错误
- 使用 c++ 生成文件的文件链接
- 生成文件:链接器错误
- 反转头文件/链接
- C++实现和头文件链接
- .cpp文件将不会与.h文件链接
- 将问题与多个文件链接在一起
- 编译三个C++文件.链接错误
- C++如何将我的主类与我的头文件链接起来
- 使文件链接问题 架构x86_64的未定义符号
- c++和mex文件链接错误
- 使用堆叠类生成文件链接问题
- 使用对象文件链接c++中的东西