Problem with compiling header and cpp file on macOS Catalina in clang++ - c++

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

Related

Cannot Configure CLion for Linking C++

I have a simple c++ example that I am trying to compile on a MacOSX machine using the CLion IDE. The goal is to use header files and cmake to compile this code on CLion with an aim to expand to something bigger and so for now, I have simplified my code to have a bar.h header file and a bar.cpp implementation file. I am getting compilation issues during linking. My code looks as follows:
main.cpp
#include "bar.h"
using namespace std;
int main(int argc, char** argv){
cout<<"starting"<<endl;
Bar bar = Bar("Using Bar Function From Main");
bar.foo();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
set(CMAKE_CXX_STANDARD 11)
project(librarytest)
add_executable(myprog main.cpp)
add_subdirectory(bars)
target_link_libraries(myprog PRIVATE bars)
bars\bar.h
#pragma once
#include <string>
#include <iostream>
class Bar{
private:
std::string s;
public:
Bar(std::string s);
void foo();
};
bars\bar.cpp
#include <string>
#include <iostream>
class Bar{
private:
std::string s;
public:
Bar(std::string s){
std::cout<<"Function Bar"<<std::endl;
this->s = s;
}
void foo(){
std::cout << this->s << std::endl;
}
};
bars\CMakeLists.txt
add_library(bars OBJECT
bar.cpp
)
target_include_directories(bars PUBLIC .)
I get the following error message from my CLion CMake build (I have obfuscated by personal details/directory):
====================[ Build | all | Debug ]=====================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /{obfuscated-for-security}/testingincludes/cmake-build-debug --target all
[3/3] Linking CXX executable myprog
FAILED: myprog
: && /Library/Developer/CommandLineTools/usr/bin/c++ -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names bars/CMakeFiles/bars.dir/bar.cpp.o CMakeFiles/myprog.dir/main.cpp.o -o myprog && :
Undefined symbols for architecture x86_64:
"Bar::foo()", referenced from:
_main in main.cpp.o
"Bar::Bar(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
I have searched other Stack Overflow articles on similar errors but none can explain the issue as it pertains to linking on the OSX. I have followed some of the guidance like re-installing the compiler.
Can anyone suggest how to get this working on the MacOSX using CLion? Any pointers or suggestions would be welcomed.
The problem is not the build (AFAIK), it's the code.
Your bar.cpp file should look like this
#include "bar.h"
#include <string>
#include <iostream>
Bar::Bar(std::string s){
std::cout<<"Function Bar"<<std::endl;
this->s = s;
}
void Bar::foo(){
std::cout << this->s << std::endl;
}
Your version duplicates the Bar class definition in both the header and cpp file. My version includes the Bar class definition and just adds the definitions of the constructor and member function.

(Sublime text) Undefined symbols for architecture arm64

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

Undefined symbols for architecture x86_64: linker error

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.

Undefined symbols for architecture x86_64 -> symbol(s) not found for architecture x86_64 [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 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

C++ class method not found when compiled

I created a simple class 'Hello' in C++ using header(.h) and definition(.cpp) files. This is the header file content:
#ifndef HELLO_H
#define HELLO_H
#include <string>
namespace test
{
class Hello
{
private:
std::string name;
public:
Hello();
void say_hello();
};
}
#endif
And the definition file content is just as you expected:
#include "Hello.h"
#include <iostream.h>
using namespace test;
Hello::Hello()
{
this->name = "Yoppy Yunhasnawa";
}
void Hello::say_hello()
{
string message = "Hello, " + this->name + ".. Have nice day!";
cout << message << "\n";
}
I included this class to a main.cpp file and use it like this:
#include "Hello.h"
using namespace test;
int main(int argc, char *argv[])
{
Hello* hello = new Hello;
hello->say_hello();
}
When I compiled the main.cpp file with g++ like this,
g++ main.cpp
I got following annoying error:
Undefined symbols for architecture x86_64:
"test::Hello::say_hello()", referenced from:
_main in ccsaoOZa.o
"test::Hello::Hello()", referenced from:
_main in ccsaoOZa.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
However, that error does not appear when I don't call both constructor and say_hello method:
int main(int argc, char *argv[])
{
Hello* hello;// = new Hello;
//hello->say_hello();
}
I use macport GCC 4.7 and I am very sure that my method is there but why this symbol(s) not found error keep appearing? Please show me my mistake. Thank you.
When you invoke g++ main.cpp, compiler performs both compiling AND linking. But the code cannot be linked without Hello.cpp file. So, you have two options: either compile and link separately:
g++ -c main.cpp
g++ -c hello.cpp
gcc main.o hello.o
or compile and link everything at the same time:
g++ main.cpp hello.cpp