Home  >  Article  >  Backend Development  >  How to solve C++ compilation error: 'undefined reference to 'namespace::function''?

How to solve C++ compilation error: 'undefined reference to 'namespace::function''?

PHPz
PHPzOriginal
2023-08-26 23:01:521358browse

解决C++编译错误:\'undefined reference to \'namespace::function\'\',如何解决?

Solution to C compilation error: 'undefined reference to 'namespace::function'', how to solve it?

When writing programs in C, we often encounter some compilation errors. One of the common errors is 'undefined reference to 'namespace::function', which means that the definition of the function cannot be found during the linking phase. This error usually occurs when we call functions defined in other source files or libraries. This article will focus on this error and introduce several possible solutions.

Before we start to solve this error, let's take a look at a simple code example:

// file1.cpp

#include <iostream>

void foo();

int main() {
  foo();
  return 0;
}
// file2.cpp

#include <iostream>

void foo() {
  std::cout << "Hello, world!" << std::endl;
}

In the above example, we define a function foo(), and It is called in the main() function. We can save these two code files as file1.cpp and file2.cpp respectively.

When we try to compile and link these two files together, we are likely to encounter the following error:

undefined reference to `foo()'

So, how to solve this error? Here are several possible solutions:

  1. Compile and link all source files together:
    This is the simplest solution, just compile the two source files together and link. Taking the GNU compiler as an example, we can use the following command to compile the two files together:

    g++ file1.cpp file2.cpp -o program

    and then use ./program to run the generated executable file.

  2. Using the declaration of the function:
    If we put the declaration of the foo() function at the beginning of the file1.cpp file, This will solve the compilation error. Modify file1.cpp as follows:

    // file1.cpp
    
    #include <iostream>
    
    void foo(); // 在这里添加函数声明
    
    int main() {
      foo();
      return 0;
    }

    Then compile and link the two files together, and the program can be successfully executed.

  3. Using header files:
    We can put the declaration of the foo() function in a separate header file, and then in file1. cpp and file2.cpp contain this header file respectively. Modify the code as follows:

    // file1.cpp
    
    #include <iostream>
    #include "functions.h" // 包含头文件
    
    int main() {
      foo();
      return 0;
    }
    // file2.cpp
    
    #include <iostream>
    #include "functions.h" // 包含头文件
    
    void foo() {
      std::cout << "Hello, world!" << std::endl;
    }

    functions.hThe content of the header file is as follows:

    // functions.h
    
    void foo(); // 函数声明

    Then compile and link the two files together, and the program can be successfully executed.

Through the above three methods, we can successfully solve the 'undefined reference to 'namespace::function' compilation error. Of course, you may encounter other more complex situations in actual programming, but the ideas for solving the problem are similar: ensure that the definition and declaration of the function are visible where they need to be called.

Summary:
When we encounter the error 'undefined reference to 'namespace::function' in C programming, we must first make it clear that this error occurs during the linking phase, indicating that the function cannot be found. Definition. We can solve this error by compiling and linking all source files together, using function declarations, using header files, etc. Note, when troubleshooting errors, make sure that function definitions and declarations are visible where they are called.

The above is the detailed content of How to solve C++ compilation error: 'undefined reference to 'namespace::function''?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn