#How to solve the 'error LNK2019' problem
19 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.
AimEM.obj : error LNK2019: 无法解析的外部符号 "struct ifr::Config::ConfigController __cdecl ifr::Config::createConfig<struct EM::AimEM::AimOffset>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,struct EM::AimEM::AimOffset *,struct ifr::Config::ConfigInfo<struct EM::AimEM::AimOffset> const &)" (??$createConfig@UAimOffset@AimEM@EM@@@Config@ifr@@YA?AUConfigController@01@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEAUAimOffset@AimEM@EM@@AEBU?$ConfigInfo@UAimOffset@AimEM@EM@@@01@@Z),函数 "public: __cdecl EM::AimEM::AimEM(bool)" (??0AimEM@EM@@QEAA@_N@Z) 中引用了该符号 [...\cmake-build-debug\XXX.vcxproj]
...\cmake-build-debug\Debug\XXX.exe : fatal error LNK1120: 1 个无法解析的外部命令 [...\cmake-build-debug\XXX.vcxproj]
Declaration and implementation
template<class T>
ConfigController createConfig(const std::string &name, T *data, const ConfigInfo<T> &info);
template<class T>
ConfigController createConfig(const std::string &name, T *data, const ConfigInfo<T> &info){
//some code
}
are you putting template implementation in a source file instead of a header?
I have written in both places.
typically template needs to be in header file, refer to this, https://stackoverflow.com/questions/115703/storing-c-template-function-definitions-in-a-cpp-file
I had this problem when I was compiling my project with clang, but linking with ld and not lld
check your linker settings and see if you're mixing g++ and clang
Hmm, I'm sorry. I don't know where to check. I use Clion, maybe this ?
did you try what I suggested?
#ifndef IFR_OPENCV_CONFIG_HPP
#define IFR_OPENCV_CONFIG_HPP
namespace ifr {
namespace Config {
struct ConfigController {
std::function<void()> save;//save config
std::function<void()> load;//load config
};
template<class T>
struct ConfigInfo {
std::function<void(T *, rapidjson::Writer<StringBuffer> &)> serialize;
std::function<void(T *, const rapidjson::Document &)> deserialization;
};
template<class T>
ConfigController createConfig(const std::string &name, T *data, const ConfigInfo<T> &info);
};
} // ifr
#endif //IFR_OPENCV_CONFIG_HPP
My header file is similar to this. It is different from that in the link. It is not a class
😭
Since youre not really giving enough information I'd just start commenting stuff out untill it compiles and then start adding stuff back to see exactly where compilation fails.
Well, I finally understand that it seems that my current compiler (C++14) cannot separate the declaration and implementation of template functions unless I use C++20.
Thank you and let us know if you have any more questions!