Ramesh
Ramesh

Reputation: 2337

How to make a library in c++ in linux

I am writing a small application in c++ and I have some questions regarding that. I am basically a Java developer now moving into c++.

  1. If I use some library like boost, curl etc. can I make it run without installing that on the client machine (I mean something like including all library jar files inside the project in Java)

  2. I have installed some library or software in linux. After that if I type in the terminal it pings the software. For example php, after you install it you can use php from terminal. How does this work? Can I use my simple c++ project to do so?

Upvotes: 0

Views: 477

Answers (3)

Praveen Felix
Praveen Felix

Reputation: 1428

1)Answer to your Q1 is compilation with libraries statically linked. For example with gcc Compiler:

# gcc -static myfile.c -o myfile

2)Answer to you Q2 is appending the absolute path of executable to $PATH Environment Variable. For example in Bash shell:

# export PATH=${PATH}:/home/user/pathofexecutable

The above setup will be temporary only for that terminal you do. To make it available to all terminal in you machine add the above export command to /home/user/.bashrc file.

Upvotes: 2

Mike DeSimone
Mike DeSimone

Reputation: 42825

  1. Yes. You use a process called static linking, which links all the libraries into one big executable. In ./configure scripts (from autotools), you use the --enable-static flag. When building your program, you use the -static flag. The static libraries are the ones with the .a suffixes; shared libraries use .so, sometimes with a version number suffix).

  2. PHP is not a library, it is a language (i.e. executable) which provides its own command-line interface. Your C++ executable can work similarly, you just have to get the input from cin (in <iostream>) and write results to cout, using cerr for error messages.

Your title question, "How to make a library in c++ in linux" (as opposed to using a library): You use the ar program to link several .o files into a single .a library file. You can also use ranlib to clean up the .a file. Read the man pages for those commands to see how they are used.

Upvotes: 3

Ariel
Ariel

Reputation: 26783

For question 1, you want to compile the program as a static executable. (Just pass -static to g++.) It will make the program much larger since it needs to include a copy of stuff normally kept as libraries.

For question 2 I'm pretty sure what you mean is having a program in the PATH. Type echo $PATH to see the path on your current machine. If you install your program in one of those directories it will run from anywhere. (Most likely /usr/local/bin/)

Upvotes: 1

Related Questions