I am new to C++. I have a problem with including the header.
I'm trying to compile my a.cpp file and get this error. I'm using Sublime Text as a text editor. Can anyone help me with this problem? Thank you in advance!
Undefined symbols for architecture arm64:
"print()", referenced from:
_main in a-3ccfb2.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here are my file:
/* Filename: a.cpp */
#include <iostream>
#include "b.h"
using namespace std;
int main() {
print();
return 0;
}
/* Filename: b.h */
#ifndef _b_h
#define _b_h
void print();
#endif
/* Filename: b.cpp */
#include <iostream>
#include "b.h"
using namespace std;
void print() {
cout << "Hello, world!" << endl;
}
I compile my cpp with: (this works fine except when I try to include my header file - example: #include "b.h")
g++ -std=c++17 -Wall a.cpp -o a
g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Related
I've been trying tu run this code but I'm keep getting this long error message. I think the code is okay, because it's working on my Windows computer, so I think it's a problem with my compiler. I am using VisualStudioCode on macOS Catalina 10.15.7 and clang 12.0.0.
This is my main.cpp file
#include<iostream>
#include <memory>
#include "Wezel.h"
using namespace std;
int main(){
for (int i = 0; i < 10; i++) {
unique_ptr<Wezel>(new Wezel());
}
return 0;
}
Wezel.cpp file
#include "Wezel.h"
#include <iostream>
using namespace std;
Wezel::~Wezel() {
cout << "Destruct" << endl;
}
Wezel.h file
#ifndef WEZEL_H
#define WEZEL_H
class Wezel
{
public:
~Wezel();
};
#endif
The error message I get when I try to compile the main.cpp file
majkel#mbp-micha RecyclingCpp % cd "/Users/majkel/Documents/Obiektowe/RecyclingCpp/" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "/Users/majkel/Documents/Obiektowe/RecyclingCpp/"tempCodeRunnerFile
Undefined symbols for architecture x86_64:
"Wezel::~Wezel()", referenced from:
std::__1::default_delete<Wezel>::operator()(Wezel*) const in tempCodeRunnerFile-40bbc5.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You didn't tell the compiler that Wezel.cpp was part of the thing you are trying to build, so it doesn't know where to find Wezel::~Wezel().
Try like this:
g++ main.cpp Wezel.cpp
I'm trying to do a test of basic linking for cpp files, I've been searching all over and am having a lot of trouble trying to find a solution. I understand that I have to include the header in both cpp's, but I'm having trouble trying to run these two together.
//testMain.cpp
#include <iostream>
#include <stdio.h>
#include "func.h"
using namespace Temp;
int main()
{
getInfo();
return 0;
}
//func.h
#ifndef FUNC_H
#define FUNC_H
#include <iostream>
#include <stdio.h>
namespace Temp{
int getInfo();
}
#endif
//functions.cpp
#include "func.h"
using namespace std;
int Temp::getInfo()
{
return 5 + 6;
}
//error that I'm getting using VS Code
cd "/Users/jcbwlsn/Downloads/Coding/CPP/Workspace/RPG Project/src/" && g++ testMain.cpp -o testMain && "/Users/jcbwlsn/Downloads/Coding/CPP/Workspace/RPG Project/src/"testMain
Undefined symbols for architecture x86_64:
"Temp::getInfo()", referenced from:
_main in testMain-1f71a1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You're supposed to specify all translation unit files when linking a C++ program.
Your program consists of two source files, testMain.cpp and functions.cpp.
Hence the compile-and-link command should be something like:
g++ testMain.cpp functions.cpp -o testMain
Alternatively you can compile each source into separately and then link them into an executable:
g++ -c testMain.cpp -o testMain.o
g++ -c functions.cpp -o functions.o
g++ testMain.o functions.o -o testMain
Having some kind of a Makefile helps to automate this.
I'm new in cpp and when I compile two cpp files in terminal and shows the error as followed
Undefined symbols for architecture x86_64:
"func(int)", referenced from:
_main in main-45889e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
here are my codes:
main.cpp
#include <stdio.h>
#include <iostream>
#include "func.hpp"
int main(int argc, char* args[]){
func(1);
return 0;
}
func.cpp
#include "func.hpp"
#include <iostream>
int func(int a){
std::cout<<a<<std::endl;
}
func.hpp
int func(int a);
it succeed when I use Xcode to run the code while failed when using terminal to compile with the following g++ commands:
g++ -c main.cpp
g++ -c func.cpp
g++ main.o func.o -o main.out
Please help
I am studying the C++ now and I am trying to learn how to use the head file. However, there is always giving a mistake when I run the program.
/* File add.h */
#ifndef ADD_H
#define ADD_H
int add(int, int);
#endif
/* ADD_H */
/* File add.cpp */
#include "add.h"
int add(int a, int b)
{
return a + b;
}
/* File triple.cpp */
#include<iostream>
#include "add.h"
using namespace std;
int triple(int);
int triple(int x)
{
return add(x, add(x, x));
}
int main()
{
int i=0;
int j;
while (i<=5)
{ j=triple(i);
cout<<j<<endl;
//cout<<triple(j)<<endl;
i++;
}
return 0;
}
These are the 3 files I used. When I run: g++ triple.cpp
on mac, the error is given as below:
Undefined symbols for architecture x86_64:
"add(int, int)", referenced from:
triple(int) in triple-e0558f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Who can give me some hints about this mistake. Thanks a lot!
By the way, the gcc version info as below:
gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
Two way to compile:
a. Compile one-by-one
g++ -c add.cpp -o add.o
g++ -c triple.cpp -o triple.o
g++ add.o triple.o -o triple
b. Compile everything at once
g++ add.cpp triple.cpp -o triple
main.cpp
#include <iostream>
#include "Burrito.h"
using namespace std;
int main(){
Burrito b;
return 0;
}
Burrito.h
#ifndef BURRITO_H
#define BURRITO_H
class Burrito{
public:
Burrito();
};
#endif
Burrito.cpp
#include "Burrito.h"
#include <iostream>
Burrito::Burrito(){
}
Compile & Link :
lzsb$ g++ main.cpp -o main
Undefined symbols for architecture x86_64:
"Burrito::Burrito()", referenced from:
_main in ccVpCr0z.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
lzsb$
Platform:
Mac OS 10.6.8
G++ : i686-apple-darwin10 --with-gxx-include-dir=/usr/include/c++/4.2.1
You need to compile the Burrito.cpp file as well. The compiler creates object files from each .cpp file and links them afterwards. This is where your call fails, because the linker can't find the referenced Burrito class in any of your object files. To fix your compiler call just add Burrito.cpp
g++ main.cpp Burrito.cpp -o main
Your compile line should be:
g++ Burrito.cpp main.cpp -o main