You can create a static library .lib
file using the GCC compiler by following these steps:
- Compile the source files into object files:
gcc -c test.c
- Create the static library file:
ar rcs libtest.a test.o
Note that the ar
command creates an archive (a static library) from a set of object files. The options used here are:
rcs
: Create a new archive, replace existing members, and store the member names as strings.libtest.a
: The name of the archive file to be created.test.o
: The object file to be added to the archive.
The above commands will create a static library file named libtest.a
that you can link to your application. To link the library to your application, you can use the -l
option with the GCC compiler:
gcc -o test main.c -L. -ltest
In the above command, -L.
specifies the current directory as the location of the library, and -ltest
specifies the name of the library to link (without the lib
prefix and the .a
suffix).
To create a .lib
file for Windows, you need to compile your source code files into object files and then use the Microsoft Library Manager (lib.exe
) to create the library.
Here's an example of how you can create a .lib
file for Windows using the Microsoft Visual C++ compiler (cl.exe
) and the Microsoft Library Manager:
- Compile the source files into object files:
cl /c test.c
Note the /c
option is used to compile the source file into an object file without linking it.
- Create the library file:
lib /nologo /out:test.lib test.obj
Note the /out:
option is used to specify the name of the library file to be created, and the test.obj
option specifies the name of the object file to be added to the library.
The /nologo
flag suppresses display of the LIB copyright message and version number and prevents echoing of command files.
This will create a library file named test.lib
that you can link to your application. To link the library to your application, you can use the /L
and /l
options with the Microsoft Visual C++ compiler:
cl /Fe:test.exe main.c /I. /L. /ltest
In the above command, /Fe:test.exe
specifies the name of the executable file to be created, /I.
specifies the current directory as the location of the header files, /L.
specifies the current directory as the location of the library, and /ltest
specifies the name of the library to link (without the .lib
extension).
If you have Visual Studio C++ installed, you can use the Visual Studio Command Prompt to create a .lib
file.
Here's an example of how you can create a .lib
file using Visual Studio:
-
Open the Visual Studio Command Prompt. You can find it in the Start menu under the Visual Studio folder.
-
Compile the source files into object files:
cl /c test.c
Note the /c
option is used to compile the source file into an object file without linking it.
- Create the library file:
lib /nologo /out:test.lib test.obj
Note the /out:
option is used to specify the name of the library file to be created, and the test.obj
option specifies the name of the object file to be added to the library.
The /nologo
flag suppresses display of the LIB copyright message and version number and prevents echoing of command files.
This will create a library file named test.lib
that you can link to your application. To link the library to your application, you can use the /L
and /l
options with the cl.exe compiler:
less
cl /Fe:test.exe main.c /I. /L. /ltest
In the above command, /Fe:test.exe
specifies the name of the executable file to be created, /I.
specifies the current directory as the location of the header files, /L.
specifies the current directory as the location of the library, and /ltest
specifies the name of the library to link (without the .lib
extension).
You can open the Visual Studio Command Prompt in several ways, depending on which version of Visual Studio you have installed:
-
Open the Start menu and search for "Visual Studio Command Prompt". This should bring up the "Developer Command Prompt for Visual Studio" or the "Visual Studio Command Prompt" option, which you can click to open.
-
Open the Start menu and navigate to the "Visual Studio" folder. Depending on your version of Visual Studio, you may find the command prompt under a subfolder such as "Visual Studio 2019" or "Visual Studio 2017".
-
If you have Visual Studio installed, you can open the Visual Studio Developer Command Prompt from the Start menu by navigating to "Visual Studio [version] Developer Command Prompt".
-
If you have Visual Studio installed, you can open the Visual Studio Developer Command Prompt from the Start menu by navigating to "Visual Studio Tools Visual Studio Command Prompt".
Once you've opened the Visual Studio Command Prompt, you can use it to run the commands required to create a .lib
file.
For what target architecture the cl
compiler will compile to depends on the chosen cl.exe
. There are two: a x86 - and a x64.
Type in the Visual Studio Developer Command Prompt: where cl
and see where cl.exe
is located:
Example:
C:\Program Files\Microsoft Visual Studio\2022\Professional>where cl
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.34.31933\bin\Hostx86\x86\cl.exe
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64\cl.exe
Choose Hostx86\x86\cl.exe
to compile for the x86 architecture.
Choose Hostx64\x64\cl.exe
to compile for the x64 architecture.
Once you have compiled the source files into object files, you can create the ".lib" file as described in my previous answer. Note that the architecture specified when compiling the object files must match the architecture specified when linking the ".lib" file to your application.
lib /machine:x64 /out:foo.lib foo.obj
To specify the target architecture when building a static library on Linux with GCC, you can use the -march
flag. The -march
flag is used to specify the target architecture for the generated machine code.
Here's an example of how you can build a static library for an x86-64 architecture using GCC:
gcc -c -march=x86-64 test.c
The -c
option is used to compile the source file into an object file without linking it. The -march=x86-64
option specifies the target architecture for the generated machine code.
Once you have compiled the source files into object files, you can create the static library as described in my previous answer:
ar rcs test.a test.o
Note that the architecture specified when compiling the object files must match the architecture specified when linking the static library to your application.