Linking error in c++ (multiple cpp files) [duplicate] - c++

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I'm making a prime generator with 3 file (two of them are .cpp and one is .h).
However when i try to build the whole project it in onlinegdb gives this error
/tmp/ccI5GCGK.o: In function `main':
main.cpp:(.text+0x11f): undefined reference to `int primegen(int&, long*, long*)'
collect2: error: ld returned 1 exit status
main.cpp
#include <iostream>
#include "primegen.h"
int main(void)
{
//taking input for number of test cases
int test_case{2};
long int lower_lim[MAX] = {5, 15}, upper_lim[MAX] = {15, 25};
//function present in primefunc.cpp
primegen(test_case,lower_lim,upper_lim);
}
primefunc.cpp
// to make SUCCESS known to this file
extern int SUCCESS;
//main function to prime generator between limits
int primegen(int &test, auto *low, auto *up)
{
static int cases=0;
if(cases == test)
return SUCCESS;
int diff=up[cases]-low[cases];
for(int i=0;i<diff;i++)
{
//some code to be added
}
}
primegen.h
// for making arrays of lower and upper limit
constexpr int MAX = 10;
constexpr int SUCCESS = 2;
// for printing out prime number
int primegen(int &, auto *, auto *);
EDIT :- I tried moving the function from the 2nd cpp to the main.cpp and it worked and also individual builds also gives success.

Related

const causes a linker error for external file [duplicate]

This question already has answers here:
Why does const imply internal linkage in C++, when it doesn't in C?
(7 answers)
Closed 1 year ago.
I have this cpp file which contains 2 arrays:
const float arr[123]={/*..*/};
const float ave[213]={/*..*/};
And in my main.cpp I have:
int main()
{
extern float arr[123];
float temp[123];
for(unsigned i=0; i<123;i++)
temp[i]=arr[i];
return 0;
}
When I build my project I got an error saying that there’s no definition for arr.
It’s a linker error.
What could be the problem?
Your const variable has internal linkage by default.
You need to declare it as extern as well to counteract that.

Symbol already defined [duplicate]

This question already has answers here:
Why should I not include cpp files and instead use a header?
(14 answers)
Closed 2 years ago.
Having 2 simple files like that:
Main.c:
#include "Initialization.cpp"
int main() {
return 0;
}
and Initialization.cpp:
int main2() {
return 0;
}
I'm getting en error:
..."int __cdecl main2(void)" (?main2##YAHXZ) already defined in Initialization.obj...
What's peculiar when i complied the program the first time everything was ok. After recompilation this error starts appearing.
PS. I'm using Visual Studio c++ 2019
The preprocessor copies everything in the include file into Main.c which will look
int main2() {
return 0;
}
int main() {
return 0;
}
Both Initialization.o and Main.o now have definition for
main2(). Thus, you break the one definition rule and invoke undefined behavior.

undefined reference to `saving::rate' [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
static variable link error [duplicate]
(2 answers)
Closed 5 years ago.
I am write this code but when i compile this code with g++ in arch linux i recive this error
/tmp/ccG7axw1.o: In function `saving::calculate()':
saving.cpp:(.text+0x3a): undefined reference to `saving::rate'
/tmp/ccG7axw1.o: In function `saving::modify()':
saving.cpp:(.text+0x93): undefined reference to `saving::rate'
collect2: error: ld returned 1 exit status
saving.h
class saving{
private :
double savebal;
public :
saving(double newSavebal);
double calculate();
void modify();
static double rate;
};
saving.cpp
#include<iostream>
#include"saving.h"
using namespace std;
saving :: saving(double newSavebal){
savebal = newSavebal;
}
double saving :: calculate(){
savebal += (savebal * (rate / 100))/12;
}
void saving :: modify(){
cout<<"Please enter the new rate"<<endl;
cin>>rate;
}
mainSaving.cpp
#include<iostream>
#include"saving.h"
using namespace std;
void menu(saving );
int main(){
saving s1(500);
menu(s1);
}
void menu(saving s){
int m;
cout<<"1) calculate month interest\n";
cout<<"2) change rate of interest\n";
cin>>m;
switch(m){
case 1 :
s.calculate();
break;
case 2 :
s.modify();
break;
}
}
In Saving.h, you declared the static variable:
static double rate;
But you still need to define it (in other words instantiate it). To do so you should add this to Saving.cpp:
double saving::rate = 0;
Without that, the linker cannot find the actual variable, so any reference to it will result in a linker error.
Because saving is a static member, you have to have it initialized beforehand.
In your saving.cpp file, add this line after including all the headers:
double saving::rate = 0
Your code should then like this:
#include<iostream>
#include"saving.h"
using namespace std;
double saving::rate = 0; //With this line here
saving :: saving(double newSavebal){
savebal = newSavebal;
}

Generic Cuda function - Template & Cuda - c++ [duplicate]

This question already has answers here:
CUDA linker error with template class
(1 answer)
Why can templates only be implemented in the header file?
(17 answers)
Closed 7 years ago.
My goal is to make a generic Cuda Kernel. My first step is trying to use templates in the function cudaMain (not yet in the Kernel - this will be my second step). cudaMain is called from my c++ main() file. From cudaMain the Kernel is called. This works fine, so long as I don't use templates. As soon as I add the templates to the class and cudaMainI get this error:
undefined reference to 'Cuda_class<int>::cudaMain(int, int, int*, int*, int*, int*, int*)'
Here's the code:
main.cpp:
#include "cuda_class.hpp"
Cuda_class<int> p;
p.cudaMain(trees.size(), trees[0].size(), treeArray_x, treeArray_y, treeArray_z, treeArray_ID, box);
cuda_class.hpp:
template <class T>
class Cuda_class{
public:
void cudaMain(int number_of_trees, int tree_size, T treeArray_x[], T treeArray_y[], T treeArray_z[], int treeArray_ID[], T box[]);
};
cuda_class.cu:
#include "cuda_class.hpp"
__global__
void insideBox(int *treeArray_x, int *treeArray_y, int *treeArray_z, int *treeArray_ID, int *box, int tree_size){
//for each thread has it's own tree starting here
int startOfTree = threadIdx.x * tree_size ;
int endOfTree = startOfTree + tree_size - 1;
traverseTree(treeArray_x, treeArray_y, treeArray_z, treeArray_ID, box, 1, startOfTree, endOfTree);
}
template <class T>
void Cuda_class<T>::cudaMain(int number_of_trees, int tree_size, T treeArray_x[], T treeArray_y[], T treeArray_z[], int treeArray_ID[], T box[]){
cudaSetDevice(MYDEVICE);
// do something allocate memory etc
//launch kernel
insideBox<<<1,32>>>(d_treeArray_x, d_treeArray_y, d_treeArray_z, d_treeArray_ID, d_box, tree_size);
//do some other stuff
}

Undefined reference when using a function included in a header file [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I am experiencing something weird with my c++ source file or perhaps the compiler itself. It seems that when I attempt to compile the file, it hits me with a message -
undefined reference to "Basic_int_stack::Basic_int_stack()
undefined reference to "Basic_int_stack::Push(int)
Here is my code (I'm still a beginner so don't expect any crazy professional code )
Header file:
class Basic_int_stack
{
public:
// This part should be implementation independent.
Basic_int_stack(); // constructor
void push( int item );
int pop();
int top();
int size();
bool empty();
private:
// This part is implementation-dependant.
static const int capacity = 10 ; // the array size
int A[capacity] ; // the array.
int top_index ; // this will index the top of the stack in the array
};
Implementations:
#include "basic_int_stack.h"// contains the declarations of the variables and functions.
Basic_int_stack::Basic_int_stack(){
// the default constructor intitializes the private variables.
top_index = -1; // top_index == -1 indicates the stack is empty.
}
void Basic_int_stack::push( int item ){
top_index = top_index + 1;
A[top_index] = item ;
}
int Basic_int_stack::top(){
return A[top_index];
}
int Basic_int_stack::pop(){
top_index = top_index - 1 ;
return A[ top_index + 1 ];
}
bool Basic_int_stack::empty(){
return top_index == -1 ;
}
int Basic_int_stack::size(){
return top_index;
}
Main Function:
#include "basic_int_stack.h"
#include <iostream>
int main()
{
int var;
Basic_int_stack s1;
while((std::cin >> var)>=0){
s1.push(var);
}
return 0;
}
This is happening because you're building your main file without building and linking your class implementation file as well. You need to adjust your build settings somehow.
It is because you don't include Basic_int_stack.cpp when you complile.
Simplely speaking, when you encounter Undefined reference to xxx, it is a error generated by linker, when means the compliler can't find the implements. So you need check if you include the cpp file or dynamic library or static library.
I faced the same problem. Finally I found a fix by including .cpp file in the main file.
#include "file_name.cpp" //In the main file