Skip to main content
ubuntuask.com

Back to all posts

How to Use A Function Defined In Another File With G++?

Published on
4 min read
How to Use A Function Defined In Another File With G++? image

Best C++ Development Guides to Buy in October 2025

1 C++ Programming Language, The

C++ Programming Language, The

  • AFFORDABLE PRICES: SAVE MONEY WITH QUALITY USED BOOKS!
  • ECO-FRIENDLY CHOICE: PROMOTE SUSTAINABILITY BY REUSING RESOURCES.
  • DIVERSE SELECTION: FIND HIDDEN GEMS AND RARE EDITIONS EASILY!
BUY & SAVE
$79.43 $89.99
Save 12%
C++ Programming Language, The
2 Beginning C++ Game Programming: Learn C++ from scratch by building fun games

Beginning C++ Game Programming: Learn C++ from scratch by building fun games

BUY & SAVE
$29.99 $49.99
Save 40%
Beginning C++ Game Programming: Learn C++ from scratch by building fun games
3 Programming: Principles and Practice Using C++ (C++ In-depth)

Programming: Principles and Practice Using C++ (C++ In-depth)

BUY & SAVE
$79.99
Programming: Principles and Practice Using C++ (C++ In-depth)
4 C++ Primer (5th Edition)

C++ Primer (5th Edition)

BUY & SAVE
$51.05 $69.99
Save 27%
C++ Primer (5th Edition)
5 Modern C++ Programming Cookbook: Master Modern C++ with comprehensive solutions for C++23 and all previous standards

Modern C++ Programming Cookbook: Master Modern C++ with comprehensive solutions for C++23 and all previous standards

BUY & SAVE
$25.55 $54.99
Save 54%
Modern C++ Programming Cookbook: Master Modern C++ with comprehensive solutions for C++23 and all previous standards
6 C++: The Comprehensive Guide to Mastering Modern C++ from Basics to Advanced Concepts with Hands-on Examples, and Best Practices for Writing Efficient, Secure, and Scalable Code (Rheinwerk Computing)

C++: The Comprehensive Guide to Mastering Modern C++ from Basics to Advanced Concepts with Hands-on Examples, and Best Practices for Writing Efficient, Secure, and Scalable Code (Rheinwerk Computing)

BUY & SAVE
$54.18 $69.95
Save 23%
C++: The Comprehensive Guide to Mastering Modern C++ from Basics to Advanced Concepts with Hands-on Examples, and Best Practices for Writing Efficient, Secure, and Scalable Code (Rheinwerk Computing)
7 C++ All-in-One For Dummies

C++ All-in-One For Dummies

BUY & SAVE
$26.62 $44.99
Save 41%
C++ All-in-One For Dummies
8 Learn C++ Quickly: A Complete Beginner’s Guide to Learning C++, Even If You’re New to Programming (Crash Course With Hands-On Project)

Learn C++ Quickly: A Complete Beginner’s Guide to Learning C++, Even If You’re New to Programming (Crash Course With Hands-On Project)

BUY & SAVE
$14.21 $23.95
Save 41%
Learn C++ Quickly: A Complete Beginner’s Guide to Learning C++, Even If You’re New to Programming (Crash Course With Hands-On Project)
+
ONE MORE?

To use a function defined in another file with g++, you need to follow a few steps.

First, make sure the function is declared in a header file (.h) that is included in both the file where the function is defined and the file where you want to use the function.

Next, compile the file where the function is defined using g++. This will generate an object file (.o).

Then, compile the file where you want to use the function, along with the object file generated in the previous step. This will create an executable that includes the function from the other file.

Make sure to link the object file when compiling the file that uses the function by adding it to the g++ command, like this:

g++ file1.cpp file2.cpp -o output -I /path_to_header_file

Now you should be able to use the function defined in the other file in your current file.

What is the process of creating a shared library in g++?

Creating a shared library in g++ involves several steps:

  1. Write the code for the functions you want to include in the shared library.
  2. Compile the source files into object files using the -c flag. For example: g++ -c -fPIC library.cpp g++ -c -fPIC main.cpp
  3. Create the shared library by linking the object files with the -shared flag: g++ -shared -o liblibrary.so library.o
  4. Optionally, you can create a symbolic link with a version number to the shared library: ln -s liblibrary.so liblibrary.so.1.0
  5. (optional) Specify the path to the shared library using the LD_LIBRARY_PATH variable or by adding it to the ldconfig configuration file.
  6. To compile an application using the shared library, specify the library name and path using the -L and -l flags: g++ -o main main.o -L. -llibrary
  7. Run the executable and ensure that it can access and use the functions from the shared library.

By following these steps, you can easily create and use a shared library in g++ for your projects.

How to use a static library in g++?

To use a static library in g++, follow these steps:

  1. Compile the static library: First, you need to have a static library (.a file) that you want to use. If you don't have one, you can create it by compiling your source files into object files and then archiving them into a static library using the ar command.

ar rcs libmylibrary.a file1.o file2.o

  1. Compile your program: Now, you need to compile your program source code using the g++ compiler and link it with the static library you created in the previous step.

g++ -o myprogram main.cpp -L/path/to/lib -lmylibrary

In this command:

  • -o myprogram specifies the name of the executable output file.
  • main.cpp is your program source code file.
  • -L/path/to/lib specifies the directory where your static library is located.
  • -lmylibrary links your program with the static library named libmylibrary.a.
  1. Run your program: Once your program is successfully compiled and linked with the static library, you can run it by executing the generated executable file.

./myprogram

How to compile multiple source files in g++?

To compile multiple source files in g++, you can follow these steps:

  1. Create multiple source files with the .cpp extension. For example, file1.cpp, file2.cpp, file3.cpp.
  2. Open your terminal or command prompt and navigate to the directory where your source files are located.
  3. Use the g++ command to compile all the source files into object files. For example:

g++ -c file1.cpp file2.cpp file3.cpp

This will compile each source file into a corresponding object file (file1.o, file2.o, file3.o).

  1. Once all the source files have been compiled into object files, you can link them together to create an executable file using the g++ command. For example:

g++ file1.o file2.o file3.o -o output

This will link all the object files together and create an executable file named "output".

  1. Run the executable file using the following command:

./output

By following these steps, you can easily compile multiple source files in g++ and create an executable file.

What is the purpose of the -I flag in g++?

The -I flag in g++ is used to specify the directories where the compiler should look for header files during the compilation process. This flag allows you to include additional directories, other than the default system directories, in the search path for header files. By specifying the directories with the -I flag, you can ensure that the compiler can find and include the necessary header files for your program to compile successfully.