c++ - 如何在 C 中编写翻译单元?
问题描述
基本上我想要做的是C等价于:
翻译单元列表.cpp
std::vector<std::string> list;
bool addListItem(std::string str){
list.push_back(str);
return true;
}
int main(int argc, char **argv){
for(std::string translation_unit_name : list){
cout << translation_unit_name << endl;
}
return 0;
}
一些文件.cpp
static bool initialized = addListItem("some file!");
some_file2.cpp
static bool initialized = addListItem("some other file!");
这样,translation_unit_list 就知道编译成最终可执行文件的每个翻译单元。这允许用户有选择地编译他们想要包含的内容。有没有办法用c实现相同的结果?
示例所需的行为:
g++ -o exec1 translation_unit_list.cpp some_file.cpp
./exec1
some file!
g++ -o exec1 translation_unit_list.cpp some_file2.cpp
./exec1
some other file!
g++ -o exec1 translation_unit_list.cpp some_file.cpp some_file2.cpp
./exec1
some file!
some other file!
解决方案
推荐阅读
- android - 在没有使用 nRF Connect 的解决方法的情况下,无法使用自定义应用程序连接到 BLE 设备
- python - Discord.py task.loop 用于更改 cog 不工作的状态
- javascript - 如何使此图像地图元素具有响应性?
- c++ - 将从命令行翻译的库链接到 Make
- html - 未应用 Google 字体。我正在尝试将 Google 字体应用于容器内的文本
- c# - 在 Blazor 组件中使用 for 循环获取 ArgumentOutOfRangeException
- reactjs - 本地服务器问题,启动反应应用程序
- solaris - 如何在 Solaris 中查找和替换字符串?
- xamarin.forms - Xamarin.Forms 使 WebView 可编辑
- python - Python 不读取 unicode U + FE0F