C++ #include不起作用

Fra*_*cis 0 c++ header include

我试图在C++中包含这样的函数,我无法理解为什么它不起作用.我有3个文件.

test.cc

int test() {
  std::cout << "This is a test" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

test.h

int test();
Run Code Online (Sandbox Code Playgroud)

main.cc

#include "test.h"

int main() {

    test();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误和我使用的命令.

c++ main.cc -o main
Undefined symbols for architecture x86_64:
  "test()", referenced from:
      _main in main-12ba52.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

小智 5

这是罪魁祸首.

c ++ main.cc -o main

你需要链接test.o.

c ++ main.cc test.cc -o main