# New to programming on Linux

This topic is 2693 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello. I am a college student. I know C and C++ and so far I have been practicing them on WIndows on my own and on Linux (Cent OS) at university.

On Linux, I know how to create a makefile and make some targets. But how do I specify to the compiler to create a lib file? (I know it's not a .lib like Windows. I mean the corresponding file type on Unix systems).
Also, how do I include a directory ? E.g I want to include the headers in SDL. How do I do that?
And last, how do I link libraries (those that I have made or others have). In visual studio it is #pragma comment(lib, "libName.lib")

Edit: I forgot to mention I am using g++

##### Share on other sites
You can send options to the linker using the '-l' switch (that's a lower case L, not a capital i) and specify include folders using -I (a capital i) aswell as tons of other command line options that GCC uses. If you're not familiar with a *nix environement you'll have to count on relearning alot compared to windows My suggestion for you is to install an IDE like Code::Blocks, CodeLite, QtCreator, Anjuta, KDEvelop or Netbeans, then you don't have to worry about makefiles atleast.

gcc sdl-config --cflags yourfile.cpp sdl-config --libs

or you can set the compiler/linker options manually:

gcc -I/usr/include/SDL yourfile.cpp -lSDL

##### Share on other sites

On Linux, I know how to create a makefile and make some targets. But how do I specify to the compiler to create a lib file? (I know it's not a .lib like Windows. I mean the corresponding file type on Unix systems).

It's not the compiler that creates a lib file (the proper generic name is "static library"), but rather the librarian. The librarian for the Visual C++ toolchain is "lib.exe". The librarian that's typically used with the GCC toolchain is called "ar". The filename extension for static libraries on UNIX is typically ".a".

ar is quite simple to use. Do "man ar" in a terminal and that will tell you how to use it.

Also, how do I include a directory ? E.g I want to include the headers in SDL. How do I do that?
[/quote]
You probably want to look up the "-I" compiler switch, which is similar to "/I" for the Visual C++ compiler.

And last, how do I link libraries (those that I have made or others have). In visual studio it is #pragma comment(lib, "libName.lib")
[/quote]
Another way to link libraries using the Visual C++ toolchain is to omit the #pragma and pass the libraries on the command line to the linker, which would be link.exe in this case (though link.exe it is also invokable via the cl.exe frontend).

And indeed this is also the way to do it with the GNU toolchain. You can either specify the path to the static library on the linker command line, or specify the library's directory with the -L switch and the the library's name following the -l switch.

So if you have libFoo.a (the "lib" prefix is standard for static libraries on UNIX) in foo/bar/, then you can either do:

 g++ <object files> -Lfoo/bar -lFoo <other libraries> -o <executable> 

or

 g++ <object files> foo/bar/libFoo.a -o <executable> 

Different sources will give varying reasons for which form you should prefer. I tend to use the first form for system libraries (where no -L is needed, as the system library folders are searched automatically), and the second form for everything else i.e. for all libraries I've built from source.

A couple of related points and some background info:

"g++" is actually what's known as a "frontend", even though it's colloquially referred to as a "compiler". The g++ frontend is capable of launching both the compiler and the linker. In the above examples, we're using it to launch the linker. The 'real' linker executable is actually called "ld" in the GNU toolchain, though you usually won't have to be aware of that.

Secondly, when specifying static libraries on the g++ linker command line, the order in which they appear is usually significant. If libA.a calls functions defined in libB.a, then libA.a should appear before libB.a on the command line. This behaviour differs from the Visual C++ linker, which as far as I know allows you to specify the libraries in any order.

The GNU linker will look through the libraries in the order specified to find the symbols needed to link the binary together. Doing it this way lowers runtime memory usage as only one library needs to be resident at a time. If you specify the libraries in the wrong order, you'll get a linker error about missing symbols. On today's machines with gigabytes of RAM this can be annoying if your build system is unable to order the libraries correctly. As a workaround, you can add the "-Wl,--start-group" and "-Wl,--end-group" linker arguments around the libraries on the command line in order to tell the linker that you're too lazy to work out the correct order But IMHO, getting the order right is probably worth doing as it ensures you don't have any mutually dependent libraries.

##### Share on other sites
Thank you very much for your replies!

Where are library folders placed in linux? On windows I used to put the in C://, but Linux seems to deny this operation.

---

I put them in my home folder to check if it's going to work.

This is the makefile:
main: main.cpp
g++ -L~irrlicht-1.7.2/lib/Linux -I~irrlicht-1.7.2/include -llibIrrlicht.a main.cpp -o test

clean:
rm test

And I get a linker error message:
g++ -L~irrlicht-1.7.2/lib/Linux -I~irrlicht-1.7.2/include -llibIrrlicht.a main.cpp -o test
/usr/bin/ld: cannot find -llibIrrlicht.a
collect2: ld returned 1 exit status
make: *** [main] Error 1

Why is that? I have specified the library search path

Edit: I was compiling an empty C++ file. When I tried to compile the correct one I got a message that it can't find the header file as well - meaning that the "include" path didn't work as well...

##### Share on other sites
The path "~irrlicht" means "the home directory of the user "irrlicht". I suspect you want "~/irrlicht" (note the slash). Also, follow the instructions really carefully; it's "-lirrlicht", not "-llibirrlicht.a". Or it's "~/irrlicht-1.7.2/lib/Linux/libirrlicht.a".

You can put your own libraries wherever you want. I typically have all my code under ~/projects and a ~/projects/build where all the build output goes.

System libraries are typically in /usr/lib and /usr/local/lib. You generally shouldn't go mucking about with these directories, though, just as you shouldn't muck about with the Visual C++ installation directories on Windows.

As an aside, to avoid confusion, I'd avoid calling your binaries "test" at this stage. There's a system program called "test" (or maybe it's a builtin shell command -- can't remember) that will do nothing. Newbies trying to launch their "test" program often get confused when this is launched instead upon typing "test" instead of "./test".

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 15
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
634150
• Total Posts
3015828
×