command given by terminal:
g++ main.cpp test.cpp
Error message:
/tmp/ccvgRjlI.o: In function `test2()':
test.cpp:(.text+0x0): multiple definition of `test2()'
/tmp/ccGvwiUE.o:main.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status main.cpp
Source code:
#include "test.hpp"
int main(int argc, char *argv[])
{
test2();
return 0;
}
test.hpp
#ifndef _TEST_HPP_
#define _TEST_HPP_
#include <iostream>
void test();
void test2() { std::cerr << "test2" << std::endl; }
#endif
test.cpp
#include "test.hpp"
using std::cerr;
using std::endl;
void test() { cerr << "test" << endl; }
btw the following compiles fine:
g++ main.cpp
inlinekeyword before test2 to get around the one definition rule. - 0x5453_TEST_HPP_are reserved in C++. Lose the leading underscore. - user2100815