#How do I link static libraries?

138 messages · Page 1 of 1 (latest)

quiet crest
#

From learncpp, I found out that static libraries have .lib extention and dynamic libraries have .dll extention. So if I have a folder which contains test.lib, test.h, main.cpp, and main.cpp includes the test.h header file. How do I use g++ to compile the program?

I normally do g++ main.cpp (without external libraries)

I don't use VS, I use sublime text and g++ from cmd

merry jewelBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

quiet crest
#

add flag -static in the compile command

#

Can you write the full command please?

#

how do you do it?

#

ye?

mystic hinge
#

I wouldn't expect a library to have a main.cpp.

quiet crest
#

main.cpp is my main file

mystic hinge
#

Not illegal, ofc.
Just unexpected.

quiet crest
#

Wdym?

runic quiver
quiet crest
#

you shall add -static (for static linking) and -ltest (to link the test.lib)

quiet crest
mystic hinge
#

Hang on.
Are you asking how to link with a static library?

quiet crest
quiet crest
#

what's the full command I should use

runic quiver
#
#compilation phase
g++ -c main.cpp -I./ -o main.o

#linking phase
g++ main.o ./test.lib -o main

#run the program 
./main
#

this should work

#

-I is an include directory - where #include-s are searched

quiet crest
mystic hinge
#

-L is the directory
-l is the filename

Surely?

quiet crest
#

Umm...I use windows
so if I do g++ main.cpp, it will generate .exe and I don't think I can link it. Can I?

quiet crest
runic quiver
quiet crest
#

static linking - you tell the linker to add all the code from libraries into your application

runic quiver
#

what I wrote are the two separete steps

#

the idea is that, when useing static linking, the size of your .exe grows

#

but when you use dynamic linking (not explained here yet) the machine instructions are left in an external .dll file

quiet crest
#

shared lib with extension .lib can be compiled with other programs.
flag -shared
Produce a shared object (in this case a library) which can then be linked with other objects to form an executable.

quiet crest
runic quiver
quiet crest
#

yes

quiet crest
#

DLL is literally abbreviation of Dynamic Linked Library

#

"On Windows, dynamic libraries typically have a .dll (dynamic link library) extension, whereas on Linux, dynamic libraries typically have a .so (shared object) extension."

#

You load the functions from a DLL. No need ned to link your app to it.

mystic hinge
quiet crest
#

Ye but here I am talking bout static libs

#

shared library can be compiled dynamically (the default) and also statically by adding the -static flag

quiet crest
mystic hinge
#

No, you need -l test to link with test.lib

quiet crest
#

what does this flag doo?

#

is it L or i?

mystic hinge
#

el for Lima.

#

lower case.

quiet crest
#

ouk

#

what does -l flag do? Link?

mystic hinge
#

Yes

quiet crest
#

If I have many files, I can use cmd g++ main.cpp -l *.lib?

#

I mean if I have multiple .lib files

#

-l lib1 -l lib2 and so on

#

oukh

mystic hinge
#

Then repeat

g++ main.cpp -l test -l funky -l zigzag -o myapp.exe
quiet crest
#

you just need to pass the library name, no extension needed

#

ee a pain isn't it...but ty

#

you choose gcc LUL

mystic hinge
#

This is actually rather easy.
Venture into CMake, I dare you.

quiet crest
#

Whats cmake?

#

tho I have heard of its name, but idk whats it

runic quiver
#

a fancy build system that calls the same command behind the scenes

mystic hinge
#

Oh dear,
I opened pandora's box.

Look into my neuralizer....

quiet crest
#

eee Ima use gcc for now

runic quiver
quiet crest
#

Not now

runic quiver
#

because these theoretical examples are kinda boring

#

🙂

quiet crest
#

I wanted to use cpr but idk how to install it

runic quiver
quiet crest
#

http client library

#

I am using my phone these days for C++ cuz I can't use my Windows PC. But I am unable to install custom libraries in it (phone)

quiet crest
mystic hinge
#

Incidentally, someone may already have mentioned this, but the syntax to link with libraries is exactly the same whether is it a static lib or a dynamic lib.

quiet crest
#

ye probably monkey did it

#

-static

g++ main.cpp -static -l test -l funky -l zigzag -o myapp.exe

if you really need to link the library statically into your executable. This will make your executable slightly bigger

#

is -static flag necessary?

#

if you need the library statically linked. yes. if you already have the DLL, then no need. but the DLL must be in the same path, or in windows\system32

#

should I mention the other blackhole?

#

If I do not write the -static flag, will it generate exe + dlls?

mystic hinge
#

@Monkey, OP is linking with a library.

quiet crest
quiet crest
quiet crest
#

no no

#

I want to create a single executable

#

alright. got ya

quiet crest
#

even with the static, yes

#

even with?

#

just change the library name to the ones taht you're linking

#

what if I don't include the static flag

runic quiver
runic quiver
quiet crest
#

I need to learn about cmake

runic quiver
#

what URLs are you going to query?

quiet crest
#

Any?

#

I dont have any particular project in mind

#

I just want to learn C++ after python

runic quiver
quiet crest
#

yes

runic quiver
#

and you haven't used C++ at all until now?

quiet crest
#

Absolutely

(not at all, I have done simple arithmatics programs)

quiet crest
#

oh nah I dont need dll

runic quiver
#

@mystic hinge
can you help with setting up cmake and running hello world with this library?

quiet crest
#

What actually is cmake?

#

is it a compiler?

mystic hinge
#

It's a makefile generator.

quiet crest
#

oo

#

by the way I rather write my own make script 😅

#

Any idea how can I install c++ libraries with cmake?? (I don't have root perms so I can't add it to system folder)

runic quiver
quiet crest
#

you need a text file called CMakeList.txt

#

:'(

#

ye

#

that text file have some cmake commands that cmake can understand to create a makefile

#

Ye but it tries to install it in the system directory

#

Can I make it portable instead?

#
cmake_minimum_required (VERSION 3.2)

set (CMAKE_C_STANDARD 11)
set (CMAKE_C_STANDARD_REQUIRED ON)

project(select VERSION 1.0)
add_executable(select select.c)
#

something like that that's mine for a class I teach

#

o

quiet crest
quiet crest
quiet crest
#

or add gcc directory to your system PATH environment

#

this app uses clang compiler

mystic hinge
#

You may need to set your CMAKE_CXX_COMPILER to point at clang.exe

quiet crest
#

i m on phone

mystic hinge
#

I tend to maintain CMake presets, so I don't have to type this out all the time.

#

You can set CMAKE_CXX_COMPILER in your CMakeLists.txt too, ofc.

#

But I cannot, because my projects are X-compiled on various platforms.

quiet crest
#

actually nvm

#

ty for the question I asked in the begining

#

!solved

merry jewelBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] How do I link static libraries?