如何构建仅使用 .h 文件的 c++ 程序
How to build a c++ program with only an .h file
在GoogleTest的存储库中,有一个包含演示测试和文件的示例文件夹。在make文件夹中,有一个脚本来构建带有sample1.h
的程序和sample1.cc
文件(以及sample1_unittest.cc
)
# Builds a sample test. A test should link with either gtest.a or
# gtest_main.a, depending on whether it defines its own main()
# function.
sample1.o : $(USER_DIR)/sample1.cc $(USER_DIR)/sample1.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1.cc
sample1_unittest.o : $(USER_DIR)/sample1_unittest.cc
$(USER_DIR)/sample1.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1_unittest.cc
sample1_unittest : sample1.o sample1_unittest.o gtest_main.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@
但是,示例文件夹中的第三个示例只有一个.h
文件——sample3-inl.h
其中包含所有测试的代码,即没有 .cc 文件。根据上面的例子,我试图像下面看到的那样构建它,但是当我运行时,它说sample3.o不存在
问:当所有代码都内联在.h
文件中时,如果没有创建.o
文件,如何构建程序?
sample3.o : $(USER_DIR)/sample3-inl.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample3-inl.h
sample3_unittest.o : $(USER_DIR)/sample3_unittest.cc
$(USER_DIR)/sample3-inl.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample3_unittest.cc
sample3_unittest : sample3.o sample3_unittest.o gtest_main.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@
sample3.o : $(USER_DIR)/sample3-inl.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample3-inl.h -o sample3.o
应该这样做。 如果没有上述更改,代码将在sample3.o
规则中生成sample3-inl.o
。
或者稍后将输入.o
文件更改为 sample3-inl.o
。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 使用mongocxx驱动程序时包含头文件问题
- 使用VerQueryValue检索应用程序的文件描述
- 编译多文件C++程序
- Visual C 32位整数从文件到8位字符到文件 - 程序在某些整数上崩溃
- 文件程序不会停止显示垃圾值
- 简单的QT从文本文件程序中读取不编译
- 从文件 C++ 程序读取停止工作
- C++ - 需要有关如何正确设计多文件程序的建议
- C++文件 I/O - 程序挂起
- 简单的文件 I/O 程序C++
- 有一个简单的程序文件/程序文件(x86)指令的c++在windows
- 配置文件c++程序在perf
- 从同一文件夹读取文件(c++程序)
- 单文件程序的未定义虚函数表
- 无法用多文件程序创建SDL线程
- 我的第一个多文件C++程序不断给我错误消息
- 配置文件C++程序基于墙上的时钟时间与英特尔Vtune放大器
- c++复制文本文件程序使用头文件