0

I use g++ 10.2.0 and try to create a static library, but when I create object file for archiving a static library, object file format always shows precompiled header, it makes the final static library cannot work:

//file static_test.cpp

void fn(){
    int temp;
    ++temp;
}
//file static_test.h
void fn();

build them but not link

g++ -c static_test.h static_test.cpp -o static_test.o

use file to show static_test.o format

file static_test.o
static_test.o:GCC precompiled header (version 014) for C++

and I archive it

ar rsv libstatic_test.a static_test.o

use file to show libstatic_test.a format:

current ar archive

use a main.cpp to test this static library

#include "static_test.h"

int main(){
    fn();
    return 0;
}

compile them and 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

why and how to solve this problem, tks~

1
  • Do not compile header files. Commented Sep 9, 2021 at 4:14

2 Answers 2

3

-c is only for a single file, the second static_test.cpp is ignored. You should get the compiler warning about multiple files set to -c. g++ -c static_test.h results the precompiled header in static_test.o and static_test.cpp is ignored. The proper command should be

g++ -c static_test.cpp -o static_test.o

Do not pass header files to the compiler when you compile object files. All other commands you are using look ok.

Sign up to request clarification or add additional context in comments.

5 Comments

"-c is only for a single file, the second static_test.cpp is ignored." This is incorrect.
Official proofs?
Just try it yourself.
@n.1.8e9-where's-my-sharem. I don't have g++. clang++ reports clang: error: cannot specify -o when generating multiple output files.
you are right , -c optioin is only for one source, refer to this: stackoverflow.com/questions/16177790/…, it cannot support multiple files, when I remove header while compiling static_test.o, it's format changes to ELF file, then everything turns to ok. g++ doesn't remind me of "-c optioni only support one source" while adding multiple ".h" file then generating precompile header format file;
0

if you would like to create a static library with gcc, you have to say it to the linker/wrapper programm "gcc" like:

gcc -static -o libyourlibname(.lib/.so) obj1.o obj2.o -s

legende:

-static: tells the linker to build a static lib
-o     : output file
-s     : strip all debug/linking stuff, including debug informations

note: may be you need the option -fPIC at .c compile time like:

gcc -O2 -fPIC -c file1.c -o file1.o

legende:

-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

By the way: Pre-compiled header files are only created by compiling C/C++ files only. You have require huge memory, and mostly pre-compiled header files are not needed in small projects of small student homework tasks. And each time you change the header file, you (the compiler) have to create a new copy of the .pch file. Of course, .pch files are good for end-products which does not change it in the form for the developer. But they are mostly depend on the compiler. So, you can't use .pch files from Windows MinGW64 Project under Linux (with the near) same compiler in different versions.

2 Comments

gcc cannot create static libraries.
I try to follow your adivse, however, there are still an old problem while executing g++ -static -o libstatictest static_test.o happen static_test.o: file not recognized: , static_test.o format is GCC precompiled header (version 014) for C++, I don't understand why g++10 make static_test.o a precompiled header by default, and search a way to disable that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.