Compile CPP files using G++ in Ubuntu

Compile CPP files using G++ in Ubuntu

When I used g++ to compile the cpp file for the first time, an undefined reference error was reported. All the functions in the custom class could not be found. After searching for information, I found that the called class needed to be linked. This article uses a small example to describe the compilation process. The class2 class calls the member function of the class1 class and depends on the class1 class, while the test file calls the member function of the class2 class and depends on the class1 class.

//class1.h
#ifndef _CLASS1_H
#define _CLASS1_H
class class1
{
public:
  int f(int i);
};
#endif
//class1.cpp
#include<iostream>
#include "class1.h"
using namespace std;
int class1::f(int i)
{
  if (i==0 || i==1)
   return 1;
  else
   return f(i-1)+f(i-1);
}
//class2.h
#ifndef _CLASS2_H
#define _CLASS2_H
#include "class1.h"
class class2
{
public:
  int double_f(int i);
};
#endif
//class2.cpp
#include<iostream>
#include "class2.h"
using namespace std;
int class2::double_f(int i)
{
  class1 c;
  return 2*cf(i);
}
//test.cpp
#include<iostream>
#include "class2.h"
using namespace std;
int main()
{
  class2 c2;
  cout << c2.double_f(4) <<endl;
  return 0;
}

First compile class1, class2 and test files into .o format files

g++ -c class1.cpp
g++ -c class2.cpp
g++ -c test.cpp

Since class1 and class2 are dependent, they need to be packaged into static library files (.a format) for linking

ar -rc class1.a class1.o
ar -rc class2.a class2.o

The last step is to link test.o into an executable file

g++ -o test test.o class2.a class1.a

After execution, the executable file test is obtained, which can be executed using the ./test command. Pay attention to the order of static link libraries when linking. It is necessary to ensure the order of the dependencies in front and the back. For example, here test depends on class2, and class2 depends on class1. Then when linking, test needs to be written before calss2, and class2 needs to be written before class1, otherwise there will be an undefined reference error.

How to compile multiple files at the same time with g++

For this example, the files are placed in the /CPPPFrtory directory. Use the CD command to change into the folder containing the source files.
To compile a program by listing the files on the command line, type:

Method 1: g++ Hello.cpp Welcome2.cpp -o Welcome2.out -ansi //Two cpp source files, only one .out file is generated after compilation Method 2: Use "wildcard" to compile the program, type: g++ *.cpp -o Welcome2.out -ansi //Use * to replace all the files to be compiled in the directory

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Problems and solutions encountered when installing mininet on Ubuntu 16.04.4LTS
  • Configure VIM as a C++ development editor in Ubuntu
  • Initial settings after installing Ubuntu 16 in the development environment
  • Ubuntu terminal multi-window split screen Terminator
  • How to set up PostgreSQL startup on Ubuntu 16.04
  • Install and use Git and GitHub on Ubuntu Linux
  • Ubuntu 16.04 creates a development environment for vim and python3
  • foreman ubuntu16 quick installation
  • Install Nvidia driver in Ubuntu 18 to solve black screen and adjust resolution problem
  • Simple implementation of supporting PHP5 and PHP7 dual versions in Ubuntu
  • Install Mininet from source code on Ubuntu 16.04

<<:  Node.js sends emails based on STMP protocol and EWS protocol

>>:  Detailed explanation of MySql slow query analysis and opening slow query log

Recommend

Writing a web calculator using javascript

This article mainly records the effect of using j...

Docker Data Storage Volumes Detailed Explanation

By default, the reading and writing of container ...

Full process record of Nginx reverse proxy configuration

1. Preparation Install Tomcat on Linux system, us...

Vue implements a simple shopping cart example

This article example shares the specific code of ...

Example code for implementing transparent gradient effects with CSS

The title images on Zhihu Discovery columns are g...

vue+el-upload realizes dynamic upload of multiple files

vue+el-upload multiple files dynamic upload, for ...

Tutorial analysis of quick installation of mysql5.7 based on centos7

one. wget https://dev.mysql.com/get/mysql57-commu...

How to run Python script on Docker

First create a specific project directory for you...

Ten Experiences in Web Design in 2008

<br />The Internet is constantly changing, a...

3 ways to create JavaScript objects

Table of contents 1. Object literals 2. The new k...