So I am attempting to use the Selene Lua library (https://github.com/jeremyong/Selene). In my project, I am including the Selene header file in one of my own header files. However, I receive an error (listed below) when compiling. I do not receive the error when including the Selene library within a .cpp file. Only .hpp files. The following is some example test code:
//Test.hpp
#pragma once
#include "selene/include/selene.h"
class Test {
public: Test();
};
//Test.cpp
#include "Test.hpp"
Test::Test() { }
//main.cpp
#include "Test.hpp"
int main() {
return 0;
}
The compile error is as follows:
/tmp/ccyLHlgU.o: In function `sel::detail::append_ref_recursive(lua_State*, std::vector<sel::LuaRef, std::allocator<sel::LuaRef> >&)':
test.cpp:(.text+0x0): multiple definition of `sel::detail::append_ref_recursive(lua_State*, std::vector<sel::LuaRef, std::allocator<sel::LuaRef> >&)'
/tmp/cc3qvhbE.o:main.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
I compiled the test code using the following command:
g++ *.cpp -std=c++11 -llua
Well sir, I commend you. This problem has stumped me for days. May I ask though how making that function inline affects anything? After researching a bit, I don't quite understand the use of the keyword.
Making the function inline lets the compiler/linker know that it will encounter the (exact) same definition in multiple source files and that it is alright for that to happen. Templatized functions are implicitly inline.