This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed last month.
I have the following files,
my.h
extern int foo;
void print_foo();
void print(int);
my.cpp
#include <iostream>
#include "./headers/my.h"
void print_foo() {
std::cout << foo << '\n';
}
void print(int i) {
std::cout << i << '\n';
}
use.cpp
#include "./headers/my.h"
int main() {
foo = 7;
print_foo();
print(99);
}
building gives the following error,
/> g++ -W -std=c++11 -o output *.cpp
Undefined symbols for architecture arm64:
"_foo", referenced from:
print_foo() in my-e8b938.o
_main in use-318772.o
(maybe you meant: print_foo())
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Tried removing each definition and its references one by one and testing. It compiles fine when
void print(int)
and its references are alone. The other two defintions give a similar error shown above.
My environment: Mac M1 (Monterey), VSCode, g++ v.14
New to C++. What's going on? How do I correct?
extern int foo;
That does not define foo, it just declares it. You need exactly 1 .cpp file with int foo defined somewhere, and optionally initialized.
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 2 years ago.
I am learning c++ and the instructor made a video on how to make classes and functions in multiple files.
I have 3 simple c++ files called "main.cpp", "something.h", and "something.cpp", they are all in the same directory that has no other files. (they are below)
the problem is that the linker is throwing an error message and I really don't know why. (maybe I'm just missing something really obvious)
// main.cpp
#include <iostream>
#include "something.h"
int main(){
int a{2}, b{2};
std::cout << add(a,b) << std::endl;
int arr[5] {1,2,4,8,16};
print_arr(arr, 5);
std::cout << "Hello, world\n";
return 0;
}
// something.h
#ifndef _SOMETHING_H_
#define _SOMETHING_H_
int add(int a, int b);
void print_arr(int* arr, unsigned int size);
#endif // _SOMETHING_H_
// something.cpp
#include "something.h"
#include <iostream>
int add(int a, int b){
return a+b;
}
void print_arr(int* arr, unsigned int size){
std::cout << "{ ";
for (int i = 0; i < size; i++)
std::cout << arr << ' ';
std::cout << '}';
}
the error:
Undefined symbols for architecture x86_64:
"add(int, int)", referenced from:
_main in main-06aa98.o
"print_arr(int*, unsigned int)", referenced from:
_main in main-06aa98.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
At simplest, clang++ -Wall -g something.cpp main.cpp -o main.
You could also compile something.cpp first to create something.o...
clang++ -Wall -g -c something.cpp
...then specify to link with that when compiling main.cpp:
clang++ -Wall -g main.cpp something.o
This last approach scales better, as if you only change main.cpp you can just do the second step without recompiling something.o.
I'm new to c++ (and compiled languages in general) and am doing the drill at the end of chapter 8 in Bjarne Stroustrup "Programming and Practices using c++" but I'm getting the following error when I try to compile the code
➜ Desktop g++ -std=c++11 *.cpp -o use
Undefined symbols for architecture x86_64:
"_foo", referenced from:
print_foo() in my-4f7853.o
_main in use-46cb26.o
(maybe you meant: __Z9print_foov)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I've also tried using g++ -c my.cpp use.cpp followed by g++ -o use.exe my.o use.o but this gave the same error. The other approach I tried was g++ -c use.cpp -o use.exe, however use.exe produced no output when it ran. The source code files are
my.h
extern int foo;
void print_foo();
void print_int(int);
my.cpp
#include "my.h"
#include <iostream>
void print_foo() {
std::cout << foo << '\n';
}
void print_int(int num) {
std::cout << num << '\n';
}
use.cpp
#include "my.h"
#include <iostream>
int main() {
std::cout<<"DSGFSGFSG"<< '\n';
foo = 7;
print_foo();
int i = 99;
print_int(i);
}
I've looked at other questions that are similar (if not seemingly the same is in Link-time errors in VS 2013 while compiling the C++ program - B. Stroustrup's PPP using C++: Ch. 8 - Q1 Drill?) but the solutions haven't worked for me. Is the problem to do with my compilation using g++ or have I made a more fundamental error?
The global variable foo is only declared in your header file.
extern int foo;
You also need to define it in my.cpp
int foo;
The declaration is a promise: "it exists somewhere".
The definition actually reserves some storage for this variable.
So your linker complains because some code relying on this
promise needs to access this missing storage.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I'm learning C++, I have gotten to the point that this works:
helloworld.cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hi" << endl;
return 0;
}
I am using MacOS Mojave and for compilation I use the commands
>> g++ helloworld.cpp
>> ./a.out
This if working fine. Now I want to use header files. Therefore I've created the following files:
test.cpp
#include <iostream>
#include "add.h"
using namespace std;
int main() {
add(4,7);
return 0;
}
add.h
#pragma once
int add(int a, int b);
add.cpp
#include "add.h"
int add(int a, int b) {
return a + b;
}
and When I try to compile this I get:
>> g++ test.cpp
Undefined symbols for architecture x86_64:
"add(int, int)", referenced from:
_main in test-ebc106.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Does anyone have an idea on how to solve this?
g++ test.cpp add.cpp
Every cpp file needs to be compiled to separate .obj files
I am learning to code in C++ and am working in Xcode9.1 on OS X 10.13.1. While trying to understand the use of keyword extern, I encountered the problem that the following code:
extern int foo;
#include <iostream>
int main() {
foo = 7;
std::cout << foo << std::endl;
return 0;
}
results in a linker error when run:
Undefined symbols for architecture x86_64:
"_foo", referenced from:
_main 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)
I am not sure why the linker cannot find foo despite the definition being the first line in main.
Thanks very much for looking into my problem!
The linker cannot find foo, because it's not defined anywhere. By declaring extern int foo', you're telling the linker that the definition is somewhere else. Remove extern, or define foo somewhere where the linker can find it.
Have a look at this example on Wikipedia.
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