C++ 如何使用单个头文件将C实现文件拆分为多个文件
我有一个C/C++应用程序,带有头文件/实现文件对 之前:C++ 如何使用单个头文件将C实现文件拆分为多个文件,c++,c,g++,C++,C,G++,我有一个C/C++应用程序,带有头文件/实现文件对 之前: //=================================================== //文件名:“bitwise.cpp” // =================================================== //-->“C”特定标题: #包括 #包括 #包括 #包括 #包括 // =================================================== /
//===================================================
//文件名:“bitwise.cpp”
// ===================================================
//-->“C”特定标题:
#包括
#包括
#包括
#包括
#包括
// ===================================================
//-->相同的标题:
#包括“按位.hpp”
// ===================================================
uint8字节和
(uint8_t A,uint8_t B)
{
返回(A&B);
}
uint16\u t word\u和
(uint16\u t A,uint16\u t B)
{
返回(A&B);
}
uint32_t dword_和
(uint32_t A,uint32_t B)
{
返回(A&B);
}
//其他函数的实现
// ===================================================
//===================================================
//文件名:“按位.hpp”
// ===================================================
#ifndef按位\uuu水电站
#定义按位\uuu水电站
// ===================================================
//-->“C”特定标题:
#包括
#包括
#包括
#包括
#包括
// ===================================================
//-->相同的标题:
// ???
// ===================================================
uint8字节和
(uint8_t A,uint8_t B);
uint16\u t word\u和
(uint16_t A,uint16_t B);
uint32_t dword_和
(uint32_t A,uint32_t B);
//其他函数的原型
// ===================================================
#endif//按位\uuuu水电站
// ===================================================
主文件与此类似:
//===================================================
//文件名:“main.cpp”
// ===================================================
//-->“C”特定标题:
#包括
#包括
#包括
#包括
#包括
// ===================================================
//-->myapp标题:
#包括“按位.hpp”
// ===================================================
int main(int argc,字符**argv)
{
int ErrorCode=0;
// ...
uint8_t A=7;
uint8_t B=5;
uint8_t C=字节和(A,B);
// ...
uint16_t A=7;
uint16_t B=5;
uint16_t C=单词和(A,B);
//做点别的
// ...
uint32_t A=7;
uint32_t B=5;
uint32_t C=dword_和(A,B);
//做点别的
// ...
返回错误码;
}//int main(…)
// ===================================================
因为“bitwise.cpp”太大了,我想把它分成几个“*.cpp”文件 保留一个“bitwise.hpp”文件, 并且还需要主文件,而不必包括每个特定的实现“*.cpp”文件 之后(希望实现类似的目标):
//===================================================
//文件名:“按位_字节.cpp”
// ===================================================
//-->“C”特定标题:
#包括
#包括
#包括
#包括
#包括
// ===================================================
//-->相同的标题:
#包括“按位字节.hpp”
// ===================================================
uint8字节和
(uint8_t A,uint8_t B)
{
返回(A&B);
}
//其他函数的实现
// ===================================================
//===================================================
//文件名:“按位_word.cpp”
// ===================================================
//-->“C”特定标题:
#包括
#包括
#包括
#包括
#包括
// ===================================================
//-->相同的标题:
#包括“按位_字.hpp”
// ===================================================
uint16\u t word\u和
(uint16\u t A,uint16\u t B)
{
返回(A&B);
}
//其他函数的实现
// ===================================================
//===================================================
//文件名:“按位_dword.cpp”
// ===================================================
//-->“C”特定标题:
#包括
#包括
#包括
#包括
#包括
// ===================================================
//-->相同的标题:
#包括“按位_dword.hpp”
// ===================================================
uint32\u t word\u和
(uint32_t A,uint32_t B)
{
返回(A&B);
}
//其他函数的实现
// ===================================================
//===================================================
//文件名:“按位.hpp”
// ===================================================
#ifndef按位\uuu水电站
#定义按位\uuu水电站
// ===================================================
//-->“C”特定标题:
#包括
#包括
#包括
#包括
#包括
// ===================================================
//-->相同的标题:
// ???
// ===================================================
//在此处包括较小的实现文件(可能):
//包括“按位字节.cpp”或“按位字节.hpp”
//包括“bitwise\u word.cpp”或“bitwise\u word.hpp”
//包括“bitwise_dword.cpp”或“bitwise_dword.hpp”
// ===================================================
#endif//按位\uuuu水电站
// ===================================================
我不使用名称空间、外部变量或类声明,只使用全局函数。每个单独的实现“*.cpp”文件, 是独立于其他人的 我希望主文件保持不变,而不必修改其中的“include”。单个“*.cpp”文件可以更改,“*.hpp”也可以更改 这能做到吗? 因为,
如果(CanBeDone==TRUE)
,怎么办
我试图查看以前的帖子,但没有找到答案
[注:我没有回答完问题,我已经投了3票反对票,没有解释,看起来像
// ===================================================
// Filename: "bitwise.cpp"
// ===================================================
// --> "C" specific headers:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
// ===================================================
// --> same header:
#include "bitwise.hpp"
// ===================================================
uint8_t byte_and
(uint8_t A, uint8_t B)
{
return (A & B);
}
uint16_t word_and
(uint16_t A, uint16_t B)
{
return (A & B);
}
uint32_t dword_and
(uint32_t A, uint32_t B)
{
return (A & B);
}
// other function's implementations
// ===================================================
// ===================================================
// Filename: "bitwise.hpp"
// ===================================================
#ifndef BITWISE__HPP
#define BITWISE__HPP
// ===================================================
// --> "C" specific headers:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
// ===================================================
// --> same header:
// ???
// ===================================================
uint8_t byte_and
(uint8_t A, uint8_t B);
uint16_t word_and
(uint16_t A, uint16_t B);
uint32_t dword_and
(uint32_t A, uint32_t B);
// other function's prototypes
// ===================================================
#endif // BITWISE__HPP
// ===================================================
// ===================================================
// Filename: "main.cpp"
// ===================================================
// --> "C" specific headers:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
// ===================================================
// --> myapp headers:
#include "bitwise.hpp"
// ===================================================
int main(int argc, char** argv)
{
int ErrorCode = 0;
// ...
uint8_t A = 7;
uint8_t B = 5;
uint8_t C = byte_and(A, B);
// ...
uint16_t A = 7;
uint16_t B = 5;
uint16_t C = word_and(A, B);
// do something else
// ...
uint32_t A = 7;
uint32_t B = 5;
uint32_t C = dword_and(A, B);
// do something else
// ...
return ErrorCode;
} // int main(...)
// ===================================================
// ===================================================
// Filename: "bitwise_byte.cpp"
// ===================================================
// --> "C" specific headers:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
// ===================================================
// --> same header:
#include "bitwise_byte.hpp"
// ===================================================
uint8_t byte_and
(uint8_t A, uint8_t B)
{
return (A & B);
}
// other function's implementations
// ===================================================
// ===================================================
// Filename: "bitwise_word.cpp"
// ===================================================
// --> "C" specific headers:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
// ===================================================
// --> same header:
#include "bitwise_word.hpp"
// ===================================================
uint16_t word_and
(uint16_t A, uint16_t B)
{
return (A & B);
}
// other function's implementations
// ===================================================
// ===================================================
// Filename: "bitwise_dword.cpp"
// ===================================================
// --> "C" specific headers:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
// ===================================================
// --> same header:
#include "bitwise_dword.hpp"
// ===================================================
uint32_t word_and
(uint32_t A, uint32_t B)
{
return (A & B);
}
// other function's implementations
// ===================================================
// ===================================================
// Filename: "bitwise.hpp"
// ===================================================
#ifndef BITWISE__HPP
#define BITWISE__HPP
// ===================================================
// --> "C" specific headers:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
// ===================================================
// --> same header:
// ???
// ===================================================
// include smaller implementation files here (maybe):
// include "bitwise_byte.cpp" or "bitwise_byte.hpp"
// include "bitwise_word.cpp" or "bitwise_word.hpp"
// include "bitwise_dword.cpp" or "bitwise_dword.hpp"
// ===================================================
#endif // BITWISE__HPP
// ===================================================
// ===================================================
// Filename: "bitwise_byte.cpp"
// ===================================================
// --> "C" specific headers:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
// ===================================================
// --> same header:
#include "bitwise_byte.hpp"
// ===================================================
uint8_t byte_and
(uint8_t A, uint8_t B)
{
return (A & B);
}
// other function's implementations
// ===================================================
// ===================================================
// Filename: "bitwise.hpp"
// ===================================================
#ifndef BITWISE__HPP
#define BITWISE__HPP
// ===================================================
// --> "C" specific headers:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
// ===================================================
// --> same header:
// ???
// ===================================================
uint8_t byte_and
(uint8_t A, uint8_t B);
uint16_t word_and
(uint16_t A, uint16_t B);
uint32_t dword_and
(uint32_t A, uint32_t B);
// other function's prototypes
// ===================================================
#endif // BITWISE__HPP
// ===================================================
// ===================================================
// Filename: "bitwise.cpp"
// ===================================================
// --> "C" specific headers:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
// ===================================================
// --> same header:
#include "bitwise.hpp"
// ===================================================
// Implementation of functions removed.
// ===================================================