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 3 years ago.
I am not able to use structures for static variable and function. Could anyone explain why exactly is this behavior?
#include <iostream>
using namespace std;
class Foo
{
private:
typedef struct
{
int bee_1;
}bee;
static bee test;
public:
static void Inc(){ test.bee_1++;}
static int getBee_1(){return test.bee_1;}
};
int main(){
Foo::Inc();
Foo::Inc();
Foo::Inc();
Foo::Inc();
cout << Foo::getBee_1();
return 0;
}
I get the following error when I use this code:
clang++-7 -pthread -o main main.cpp
/tmp/main-521333.o: In function `Foo::Inc()':
main.cpp:(.text._ZN3Foo3IncEv[_ZN3Foo3IncEv]+0x24): undefined reference to `Foo::test'
main.cpp:(.text._ZN3Foo3IncEv[_ZN3Foo3IncEv]+0x2e): undefined reference to `Foo::test'
/tmp/main-521333.o: In function `Foo::getBee_1()':
main.cpp:(.text._ZN3Foo8getBee_1Ev[_ZN3Foo8getBee_1Ev]+0x7): undefined reference to `Foo::test'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
compiler exit status 1
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
Account.h file
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
private:
//attributes
double balance;
public:
//methods
void set_balance(double bal);
double get_balance();
};
#endif
Account.cpp file
#include "Account.h"
void Account::set_balance(double bal){
balance=bal;
}
double Account::get_balance(){
return balance;
}
main.cpp file
#include<iostream>
#include"Account.h"
int main(){
Account frank_account;
frank_account.set_balance(200.00);
double showBalance=frank_account.get_balance();
return 0;
}
error
C:\Users\Dell\AppData\Local\Temp\ccZOi0ua.o: In function `main':
F:/OPP/opp10/main.cpp:6: undefined reference to `Account::set_balance(double)'
F:/OPP/opp10/main.cpp:7: undefined reference to `Account::get_balance()'
collect2.exe: error: ld returned 1 exit status
Build finished with error(s).
The terminal process terminated with exit code: -1.
Please explain the error message and provide the solution.
Thank You....................................................................................................................
How did you compile these two files? This error suggests that the "Account.cpp" is not compiled or linked by you. Try this:
gcc Account.cpp main.cpp
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
Unfortunately, I couldn't find any beginner friendly tutorial on how to use giflib.
This is my code:
#include <iostream>
#include <gif_lib.h>
int main(int argc, char** argv)
{
if (argc < 2)
{
std::cerr << "No argument" << std::endl;
return 1;
}
GifFileType *gifFile = DGifOpenFileName(argv[1], NULL);
DGifSlurp(gifFile);
}
I try to compile it like this:
g++ main.cpp
but it yields this error:
/usr/bin/ld: /tmp/ccd5G2QR.o: in function `main':
main.cpp:(.text+0x5c): undefined reference to `DGifOpenFileName'
/usr/bin/ld: main.cpp:(.text+0x6c): undefined reference to `DGifSlurp'
collect2: error: ld returned 1 exit status
I'm on Ubuntu 20.04 and I've installed the libgif-dev package.
How to link the giflib library for the functions to work?
Trying randomly around I found it's:
g++ main.cpp -lgif
This question already has answers here:
Undefined Reference Compiler Error
(2 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I have a class for sorting a char array. One of the constructors takes the parameter (size_t length). When I pass it a length of type int and try to compile, I get the error
driver.cpp:(.text+0x2c): undefined reference to 'Array::Array(unsigned long)'
Is there something obvious I am missing? What can I attempt to try to get this code to compile.
I have tried creating a variable of size_t but I get the same error. I have tried changing the parameter to an integer to see if had something to do with it being type size_t, but i get the same error but for int instead.
driver.cpp:(.text+0x2c): undefined reference to 'Array::Array(int)'
Array.h
class Array{
public:
Array(size_t length);
...
...
}
Array.cpp
Array::Array (size_t length)
{
std::cout << "garbage output" << std::endl;
}
main.cpp
int main (int argc, char * argv [])
{
// TODO Add code here to test your Array class.
Array ar1(10);
return 0;
}
I expect the string "garbage output" but I receive a compilation error.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
When i write simple C++ code in X-code, it shows Linker Error.
Undefined symbols for architecture x86_64:
"Emp::id", referenced from:
Emp::Emp() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
#include <iostream>
using namespace std;
class Emp
{
public:
static int id;
int sal;
Emp()
{
Emp::id =10; // When i comment this line its working fine.
};
};
int main(int argc, const char * argv[])
{
Emp Ram;
cout << Ram.sal ;
return 0;
}
You have declared id as a static variable. You then set it in every constructor call, which is probably not what you want to do.
For a 'fix', you can add the following line above main:
int Emp::id = 0;
However, you may not want that to be static. For more information on static class variables, see this page
This question already has answers here:
Undefined reference to static class member
(9 answers)
Closed 9 years ago.
I have the following code:
class employee {
public:
static int last_id;
...
};
int main() {
employee::last_id=0;
}
When i try to run it it gives the following error:
Undefined symbols for architecture x86_64:
"employee::last_id", referenced from:
_main in chap7-F3IpS1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.3s with exit code 1]
int employee::last_id=0;
int main() {
[...]
}
You only declared the static data member but not defined it. Write before main in the global namespace
int employee ::last_id;
It will be initialized by zero though your explicitly can specify the initializer.