lundi 26 septembre 2022

Undefined symbols for architecture arm64 in template function with packed parameters [duplicate]

I have a template function that takes a variable amount of parameters (packed parameters):

// func.cpp
template<typename ...T>
void func(const std::string& arg, T... args) {
    // ...
}

and its header file looks like this:

// func.h
#pragma once
#ifndef PROJECTNAME_FUNC_H
#define PROJECTNAME_FUNC_H

#include <string>

template<typename ...T>
void func(const std::string& arg, std::string args...);

#endif //PROJECTNAME_FUNC_H

And finally, my main function looks like this:

// main.cpp
#include <string>
#include "func.h"

int main() {
  func("foo", "bar", "baz", "qux");
  return 0;
}

If I try to build my program using g++ -std=c++11 ./*.cpp I get the following error:

Undefined symbols for architecture arm64:
  "void func<>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, ...)", referenced from:
      _main in main-48d32c.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If I move the definition of func to main.cpp it works fine, so I'm assuming I'm doing something wrong in func.h, but I couldn't figure out what. I've tried using explicit instantiation of the template function as well but with no success.

My CMakeLists.txt file only has the basic commands, generated by CLion when the project was created. I've double-checked and the add_executable(<projectname> file.cpp ...) contains all the source code and header files. If it's relevant to my problem, I can provide it as well.

Aucun commentaire:

Enregistrer un commentaire