how to link header files in c++

后端 未结 4 813
时光说笑
时光说笑 2021-02-04 15:27

I\'m new to programming in C++ with header files. This is my current code:

//a.h
#ifndef a_H
#define a_H
namespace hello
{
  class A
  {
    int a;
    public:
          


        
相关标签:
4条回答
  • 2021-02-04 16:08

    You need to compile and then link both source (.cpp) files:

    g++ -Wall -pedantic -g -o your_exe a.cpp ex2.cpp
    
    0 讨论(0)
  • 2021-02-04 16:20

    Currently you are compiling and linking only ex2.cpp but this file has makes use of class def and function calls present in a.cpp so you need to compile and link a.cpp as well as:

    g++ ex2.cpp a.cpp
    

    The above command will compile the source file(.cpp) into object files and link them to give you the a.out executable.

    0 讨论(0)
  • 2021-02-04 16:21

    You don't link header files. You link object files, which are created by compiling .cpp files. You need to compile all your source files and pass the resulting object files to the linker.

    From the error message it seems you're using GCC. If so, I think you can do
    g++ ex2.cpp a.cpp
    to have it compile both .cpp files and invoke the linker with the resulting object files.

    0 讨论(0)
  • 2021-02-04 16:24

    You need to compile and link both source files, e.g.:

    g++ ex2.cpp a.cpp -o my_program
    
    0 讨论(0)
提交回复
热议问题