如果我只有依赖程序的 .o 文件,我可以在可执行文件中反映 header 文件中的更改吗
Can I reflect changes in header file in executables if i have only .o files for the dependent programs
我有两个文件
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 };
};
我编译 main.cpp 使用 -c 标志
g++ -c main.cpp 生成 main.o 文件
然后我使用
将阶乘模板的基本情况更改为 "value = 2" 和 link
g++ -o tmpex main.o tmp1.h
但是当我 运行 可执行文件时,它输出 5040 而不是 10080,这是我需要的新值。
有什么我可以做的,即使我只有 main.o 文件而不是它的源代码,我也可以更改 header 文件并在新的可执行文件中反映更改
总结
我有一个 main.o 文件和它所依赖的 header。我想在 header 文件中进行更改并在不需要 main.cpp 文件
的情况下反映新可执行文件中的更改
不,你不能。
该模板在 编译 阶段实例化,即生成 .o 文件时。
将 header 添加到链接阶段不会有任何效果
我有两个文件
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 };
};
我编译 main.cpp 使用 -c 标志
g++ -c main.cpp 生成 main.o 文件
然后我使用
将阶乘模板的基本情况更改为 "value = 2" 和 link
g++ -o tmpex main.o tmp1.h
但是当我 运行 可执行文件时,它输出 5040 而不是 10080,这是我需要的新值。
有什么我可以做的,即使我只有 main.o 文件而不是它的源代码,我也可以更改 header 文件并在新的可执行文件中反映更改
总结
我有一个 main.o 文件和它所依赖的 header。我想在 header 文件中进行更改并在不需要 main.cpp 文件
的情况下反映新可执行文件中的更改不,你不能。 该模板在 编译 阶段实例化,即生成 .o 文件时。 将 header 添加到链接阶段不会有任何效果