Gud day,

Suppose I have these files:

hello.h
hello.cpp
main.cpp

in what directory will I put the hello.o and hello.hpp so that I will only type

$ g++ main.cpp -o main

not,

$ g++ main.cpp hello.cpp -I. -o main.cpp

in the terminal..

thanks.... I will highly appreciate youre response....

Dani AI

Generated

For a reusable library you want two things in place so you never type paths at compile time: the headers must be in a system include directory and the compiled library must be in a system library directory so the toolchain finds them by default. asked exactly this; pointed out the quick-and-dirty option of keeping everything in one folder and moved toward packaging the code as a library — both right directions, but they left out a few practical install and workflow details.

Put the public header(s) where the compiler searches by default (common choices are the system include tree). Build either a static archive or a shared object for your implementation. If you install a shared object, update the runtime linker cache so programs find it without full paths. Use the system layout that matches your distro (manual installs usually go under the local tree to avoid clashing with package manager files).

If you want callers to compile without typing -I or -L flags, provide a pkg-config file that supplies the right compiler and linker flags. A minimal .pc looks like this:

prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: hello
Description: Hello library
Version: 1.0
Libs: -L${libdir} -lhello
Cflags: -I${includedir}

Install the header, library and the .pc file into the appropriate locations so users can write pkg-config --cflags --libs hello in build scripts.

For details on creating archives and shared objects see the binutils/ar notes and the runtime linker cache (ldconfig). Good references: the ar manual (tooling) and the ldconfig manual (runtime cache): https://man7.org/linux/man-pages/man1/ar.1.html and https://man7.org/linux/man-pages/man8/ldconfig.8.html. Prefer packaging (or /usr/local) for repeatable installs and watch ABI/versioning when using shared libraries.

Recommended Answers

All 6 Replies

by the way, I am using Ubuntu 6.06

put them all in the same directory as main.cpp. The compiler default is to put them in the current working directory. You can confirm that yourself by compiling a program then listing out the files in that directory.

Actually, you will still have to do the following to be able to compile and link 'main':

$ g++ -c hello.cpp -I.

Then only can you do:

$ g++ -o main main.cpp hello.o

You can try creating an archive (libhello.a) and put that in /usr/lib or /usr/local/lib before doing:

$ g++ -o main main.cpp -lhello

Hope this helps...

I mean, in what folder in the /usr directory should I put the object file of the compiled hello.cpp so that I can use that in many programs.. By the way, I am creating a library... And where will I put the hello.h as well. thanks for the reply.....

you can put it anywhere you want -- there is no restriction. Just add the -L flag or specify the whole path in the .o filename.

thank you very much for the reply, but I dont want to specify its location when I compile the main function in the terminal.

I would like to add it in the gcc library so that it would be easier for me to inherit the implementation whenever I want not specifying its location during the compilation of the main functions.


Hoping to here from you soon..... thanx again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.