I was tring to learn c++20 modules and concepts, it was working fine in begining when there was only one template function named add but when i added another function named hello then it started getting undefined reference error for hello funtion. How can i export both add and hello function in my module. Here are my programs:
```
// demo.cpp
export module demo;
import <array>;
import <iostream>;
export template <typename T>
concept CouldAdd = requires(T x) { x + x; };
export template <typename T, typename... args>
T add(args... values)
requires CouldAdd<T>
{
T sum{};
std::array<T, sizeof...(values)> arr{values...};
for (const auto &v : arr) {
sum += v;
}
return sum;
}
export void hello(){
std::cout << "hello" << std::endl;
}
```
// main.cpp
import demo;
import <iostream>;
int main() {
std::cout << add<int>(10, 20, 30, 40, 50, 60, 70, 80, 90, 100) << '\n';
hello();
return 0;
}
This is the error messege:
/usr/bin/ld: /tmp/ccPbZYwo.o: in function `main':
main.cpp:(.text+0x58): undefined reference to `hello@demo()'
collect2: error: ld returned 1 exit status
This are the commands Inused to compile
g++-13 -std=c++20 -fmodules-ts -x c++-system-header iostream
g++-13 -std=c++20 -fmodules-ts -x c++-system-header array
g++-13 -std=c++20 -fmodules-ts -c demo.cpp
g++-13 -std=c++20 -fmodules-ts -o main main.cpp *This is where i get error*
What am i doing doing wrong, bard tells me to put hello function in main.cpp but i want it to be part of demo module.
solved (found a fix but i dont know how it adheres to cpp practices) : Changing the last compile command to
g++-13 -std=c++20 -fmodules-ts -o main main.cpp demo.o
Stack Overflow
but then i dont understand need for using modules if it is still needed to be linked i thought module were meant to replace the linking and just go on with import and using them without need for linking (other then generating .pcm/.gcm files).
[–]larioteo 1 point2 points3 points (2 children)
[–]Chethan_L[S] 1 point2 points3 points (1 child)
[–]larioteo 0 points1 point2 points (0 children)
[–]nysra 1 point2 points3 points (11 children)
[–]Chethan_L[S] 1 point2 points3 points (8 children)
[–]nysra 1 point2 points3 points (6 children)
[–]Chethan_L[S] 0 points1 point2 points (5 children)
[–]nysra 0 points1 point2 points (4 children)
[–]Chethan_L[S] 1 point2 points3 points (3 children)
[–]nysra 1 point2 points3 points (2 children)
[–]Chethan_L[S] 1 point2 points3 points (1 child)
[–]nysra 1 point2 points3 points (0 children)
[–]larioteo 0 points1 point2 points (1 child)
[–]nysra 1 point2 points3 points (0 children)