+ 1

Cmake: Undefined reference to [lib_name]

I've got a cmake project setup, and the executable is giving a strange error "Undefined reference to [lib_name] at [function_call]" Note that [lib_name] is the name of the library, while [function_call] is the name of the function called from the executable. Allow me to explain how the project is setup, I've got some thing like this lib/chm/ include/chm.hpp src/chm.cpp CMakeLists.txt example/ executable.cpp CMakeLists.txt CMakeLists.txt Chm is built as a static library and linked to the executable, where is the error from? This is the code https://code.sololearn.com/cVFt78ovUMjL/?ref=app

17th Jul 2023, 5:42 PM
RuntimeTerror
RuntimeTerror - avatar
6 ответов
+ 1
It would be helpful if you provide the makefile, or any cmake config you are using. There are a lot of parts you didn't provide. Can you make a git repo with your complete source code of this test project and share the link? Or, if you're not familiar with git, then pack it into one archive, upload it somewhere and post a link.
17th Aug 2023, 9:22 PM
Евгений
Евгений - avatar
+ 1
you have to explicitly instantiate the template function by adding the following line at the end of chm.cpp: template bool chm::stringToDouble<double>(std::string const&, double&); https://godbolt.org/z/hdsa55sY8 note: you have to do that for every type T you intend to use with chm::stringToDouble<T>.
2nd Sep 2023, 1:26 PM
MO ELomari
MO ELomari - avatar
+ 1
MO ELomari Is it really so? What's the point of using template function then? I thought the purpose of a template function is opposite: to not write an implementation for every type one intends to use.
2nd Sep 2023, 3:20 PM
Евгений
Евгений - avatar
+ 1
MO ELomari I still don't get it. The whole purpose of template functions is not to write code for every possible type. Why do you say one should write it for every type needed? Could you explain more?
2nd Sep 2023, 6:11 PM
Евгений
Евгений - avatar
0
not implementation just template instantiation.
2nd Sep 2023, 5:00 PM
MO ELomari
MO ELomari - avatar
0
template instantiation ( class or function ) only needed when the definition of templates ( classes or functions ) from its declaration are separated. you can have a look at the: https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl
2nd Sep 2023, 6:26 PM
MO ELomari
MO ELomari - avatar