编译目标文件时如何不创建预编译头文件?
how can I not create precompiled header when compile object file?
我使用g++ 10.2.0并尝试创建一个静态库,但是当我创建用于归档静态库的目标文件时,目标文件格式总是显示precompiled header
,这使得最终的静态库无法工作:
//file static_test.cpp
void fn(){
int temp;
++temp;
}
//file static_test.h
void fn();
构建它们但不构建它们 link
g++ -c static_test.h static_test.cpp -o static_test.o
使用file
显示static_test.o格式
file static_test.o
static_test.o:GCC precompiled header (version 014) for C++
然后存档
ar rsv libstatic_test.a static_test.o
使用file
显示libstatic_test.a格式:
current ar archive
使用main.cpp测试这个静态库
#include "static_test.h"
int main(){
fn();
return 0;
}
编译它们并link
g++ main.cpp libstatic_test.a
libstatic_test.a: cannot add symbol: archive has no index;run ranlib to add one
collect2: error:ld return 1
为什么以及如何解决这个问题,tks~
-c
只针对单个文件,第二个static_test.cpp
被忽略。您应该收到有关多个文件设置为 -c
的编译器警告。 g++ -c static_test.h
导致 static_test.o
中的预编译 header 和 static_test.cpp
被忽略。正确的命令应该是
g++ -c static_test.cpp -o static_test.o
编译 object 个文件时,不要将 header 个文件传递给编译器。您使用的所有其他命令看起来都正常。
如果你想用 gcc 创建一个静态库,你必须像 linker/wrapper 程序“gcc”这样说:
gcc -static -o libyourlibname(.lib/.so) obj1.o obj2.o -s
传说:
-static: tells the linker to build a static lib
-o : output file
-s : strip all debug/linking stuff, including debug informations
注意:
可能是您在 .c 编译时需要选项 -fPIC,例如:
gcc -O2 -fPIC -c file1.c -o file1.o
传说:
-O2 : tells the c compiler to optimize
-fPIC : create program independet code (internal for the output code)
-c : compile C file to object file:
-o : tell the linker how the object file should be named
顺便说一句:
预编译头文件仅通过编译 C/C++ 文件创建。
您需要巨大的内存,并且在小型学生作业任务的小型项目中大多不需要预编译的头文件。
每次更改头文件时,您(编译器)都必须创建 .pch 文件的新副本。
当然,.pch 文件适用于不会为开发人员更改形式的最终产品。但它们主要取决于编译器。
因此,您不能在 Linux 下使用来自 Windows MinGW64 项目的 .pch 文件(具有接近的)不同版本的相同编译器。
我使用g++ 10.2.0并尝试创建一个静态库,但是当我创建用于归档静态库的目标文件时,目标文件格式总是显示precompiled header
,这使得最终的静态库无法工作:
//file static_test.cpp
void fn(){
int temp;
++temp;
}
//file static_test.h
void fn();
构建它们但不构建它们 link
g++ -c static_test.h static_test.cpp -o static_test.o
使用file
显示static_test.o格式
file static_test.o
static_test.o:GCC precompiled header (version 014) for C++
然后存档
ar rsv libstatic_test.a static_test.o
使用file
显示libstatic_test.a格式:
current ar archive
使用main.cpp测试这个静态库
#include "static_test.h"
int main(){
fn();
return 0;
}
编译它们并link
g++ main.cpp libstatic_test.a
libstatic_test.a: cannot add symbol: archive has no index;run ranlib to add one
collect2: error:ld return 1
为什么以及如何解决这个问题,tks~
-c
只针对单个文件,第二个static_test.cpp
被忽略。您应该收到有关多个文件设置为 -c
的编译器警告。 g++ -c static_test.h
导致 static_test.o
中的预编译 header 和 static_test.cpp
被忽略。正确的命令应该是
g++ -c static_test.cpp -o static_test.o
编译 object 个文件时,不要将 header 个文件传递给编译器。您使用的所有其他命令看起来都正常。
如果你想用 gcc 创建一个静态库,你必须像 linker/wrapper 程序“gcc”这样说:
gcc -static -o libyourlibname(.lib/.so) obj1.o obj2.o -s
传说:
-static: tells the linker to build a static lib
-o : output file
-s : strip all debug/linking stuff, including debug informations
注意: 可能是您在 .c 编译时需要选项 -fPIC,例如:
gcc -O2 -fPIC -c file1.c -o file1.o
传说:
-O2 : tells the c compiler to optimize
-fPIC : create program independet code (internal for the output code)
-c : compile C file to object file:
-o : tell the linker how the object file should be named
顺便说一句: 预编译头文件仅通过编译 C/C++ 文件创建。 您需要巨大的内存,并且在小型学生作业任务的小型项目中大多不需要预编译的头文件。 每次更改头文件时,您(编译器)都必须创建 .pch 文件的新副本。 当然,.pch 文件适用于不会为开发人员更改形式的最终产品。但它们主要取决于编译器。 因此,您不能在 Linux 下使用来自 Windows MinGW64 项目的 .pch 文件(具有接近的)不同版本的相同编译器。