如果我只有.o文件,则可以反映可执行文件中标头文件中的更改

Can I reflect changes in header file in executables if i have only .o files for the dependent programs

本文关键字:文件 可执行文件 如果      更新时间:2023-10-16

我有两个文件

main.cpp

#include<iostream>  
#include "tmp1.h"
using namespace std;
int main() {   
cout<<factorial<7>::value<<endl;  
}

和tmp1.h

#include<iostream>
using namespace std;  
template <unsigned n>  
struct factorial
{  
    enum { value = n * factorial<n-1>::value };  
};  
template <>  
struct factorial<0>  
{  
    enum { value = 1 };  
};  

i使用-c flag
编译main.cpp g -c main.cpp生成main.o文件

然后,我将阶乘模板的基本案例更改为" value = 2",然后使用
链接它g -o tmpex main.o tmp1.h

但是,当我运行可执行文件时,它会输出5040而不是10080,这是我需要的新值。

我可以做些什么,即使我只有main.o文件,而不是其源代码,我也可以更改标头文件并反映新的可执行文件中的更改

摘要

我有一个依赖的main.o文件和标头。我想对标头文件进行更改,并反映新可执行文件中的更改,而无需main.cpp文件

不,你不能。该模板是在 compilation epta中实例化的,即生成.o文件时。将标题添加到链接阶段不会有任何效果

相关文章: