C++ Boost Library Symbols not found for arm64 | M1 Macbook - c++

After installing Boost via HomeBrew and writing the following code :
#include "Common.hpp"
/* ----- B00ST ----- */
#include <boost/random/random_device.hpp>
#include <boost/random/uniform_int_distribution.hpp>
void Common::generate_random_password(std::string* password, int length) {
/*
* INPUTS :
* Pointer to store the generated password
*
* OUTPUTS :
* None
*
* RAISES :
* None
*/
std::string chars(
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890"
"!##$%^&*()"
"`~-_=+[{]}\\|;:'\",<.>/? ");
boost::random::random_device rng;
boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
for (int i = 0; i < length; ++i)
password->append(&chars[index_dist(rng)]);
} // generate_random_password(const std::string* password)
CLion's coming up with this error :
Undefined symbols for architecture arm64:
"boost::random::random_device::random_device()", referenced from:
Common::generate_random_password(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, int) in Common.cpp.o
"boost::random::random_device::~random_device()", referenced from:
Common::generate_random_password(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, int) in Common.cpp.o
"boost::random::random_device::operator()()", referenced from:
int boost::random::detail::generate_uniform_int<boost::random::random_device, int>(boost::random::random_device&, int, int, boost::integral_constant<bool, true>) in Common.cpp.o
unsigned int boost::random::detail::generate_uniform_int<boost::random::random_device, unsigned int>(boost::random::random_device&, unsigned int, unsigned int, boost::integral_constant<bool, true>) in Common.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
I've tried to reinstall Boost, double checked my CMake file but I see nothing wrong with my actual building configuration...
In case, here's my CMakeLists.txt file :
cmake_minimum_required(VERSION 3.23)
project(Banko)
set(CMAKE_CXX_STANDARD 23)
add_executable(Banko main.cpp Common.cpp Common.hpp)
find_package(Boost 1.80.0 COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(Banko ${Boost_LIBRARIES})
Thanks for your time 🕓 !
Unguest

Maybe the binaries you used was build for x86_64 architecture only.
You can check the architecture of build binaries using this command:
lipo -info /usr/lib/libName.dylib
lipo -info /usr/lib/libName.a
You can build universal libraries and check again.
Here is how you build universal binaries.
If you are using Xcode you can also exclude arm64 architecture if that fulfils your requirement.

Related

VS code Undefined symbols for architecture arm64

Here is the complete error:
Undefined symbols for architecture arm64:
"ug::UgWindow::UgWindow(int, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.cpp.o
"ug::UgWindow::~UgWindow()", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [untitled-game] Error 1
make[1]: *** [CMakeFiles/untitled-game.dir/all] Error 2
make: *** [all] Error 2
I am using cmake to build my files, i have created a UgWindow hpp file and cpp file to define its functionality here is the hpp file and cpp file.
HPP
#pragma once
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <string>
namespace ug {
class UgWindow{
public:
UgWindow(int w, int h, std::string name);
~UgWindow();
bool shouldClose(){ return glfwWindowShouldClose(window); }
private:
void initWindow();
const int width;
const int height;
std::string windowName;
GLFWwindow *window;
};
}
here is the cpp file:
#include "ug_window.hpp"
namespace ug {
UgWindow::UgWindow(int w, int h, std::string name): width{w}, height{h}, windowName{name} {
initWindow();
}
UgWindow::~UgWindow() {
glfwDestroyWindow(window);
glfwTerminate();
}
void UgWindow::initWindow() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
window = glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr);
}
}
I am trying to use vulkan with glfw and glm, im a noob with c++ and have absolutely no idea how to fix this or what the problem is.
Also this is my cmake file:
project(untitled-game)
cmake_minimum_required(VERSION 3.22.4)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -std=c++17 " )
add_executable(${PROJECT_NAME} main.cpp)
find_package(Vulkan REQUIRED)
find_package(glfw3 3.3 REQUIRED)
if (VULKAN_FOUND)
message(STATUS "Found Vulkan, Including and Linking now")
include_directories(${Vulkan_INCLUDE_DIRS})
target_link_libraries (${PROJECT_NAME} ${Vulkan_LIBRARIES} glfw)
endif (VULKAN_FOUND)
add_executable(${PROJECT_NAME} main.cpp)
Here you say that the executable program is to be built from the main.cpp source file, and only the main.cpp source file.
You need to list all your source files:
add_executable(${PROJECT_NAME} main.cpp ug_window.cpp)

Linking Error - C++ Clang MacOs

I have two projects, both built with clang++ and Xcode on MacOS.
I have a library with a header defined as follows.... (serialization.h)
#pragma once
#include <visionApp/cv/matchers/visual_database.h>
namespace visionApp {
void serializeDB(visionApp::VisualDb visualDB, std::string fileName);
visionApp::VisualDb* deserializeDB(std::string fileName);
}
The cpp file is as follows.... (serialization.cpp)
#include "serialization.h"
namespace visionApp {
void serializeDB(visionApp::VisualDb visualDB, std::string fileName)
{
}
visionApp::VisualDb* deserializeDB(std::string fileName)
{
return new visualDB();
}
}
The method is then called in another class.....
void saveRecogniser(std::string fileName)
{
serializeDB(currentVisualDB.get(), fileName);
}
void loadRecogniser(std::string fileName)
{
mVisualDatabase.reset(deserializeDB(fileName));
}
Note: currentVisualDB is a shared pointer to .get() returns a pointer.
This all builds fine. Which is great...... and make libvisionApp.a
But when i build a dependent application that can only see the headers i get the following error....
Undefined symbols for architecture x86_64:
"visionApp::serializeRecognizer(visionApp::VisualDb*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
visionApp::Recogniser::saveRecogniser(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in libvisionApp.a(planar_recogniser.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Now, this linking error suggests that clang is not able to link the serialize method, but it has no problems with the deserialize method. Removing the serialize method, and leaving only the deserialize builds correctly.
I do not understand this issue, and am unsure how to proceed. Can someone educate me as what to do in a situation like this?
Any advice on how to tackle this issue?
Note: Lipo output for the library in question.
Hal:Release daniel$ lipo -info libvisionDB.a
input file libvisionDB.a is not a fat file
Non-fat file: libvisionDB.a is architecture: x86_64
You have declared serializeDB to get first parameter by value.
void serializeDB(visionApp::VisualDb visualDB, std::string fileName);
When you call the function, you use a pointer as first parameter:
void saveRecogniser(std::string fileName)
{
serializeDB(currentVisualDB.get(), fileName); // You said that currentVisualDB is a std::shared_ptr
}
I don't know why your build gets to linking stage, it shouldn't. Maybe you have more than one place where you declare serializeDB?

Why would my simple compilation fail?

So I just install gcc from homebrew and I have a simple code:
#include <cmath>
#include <iostream>
#include <stdio.h>
int main()
{
const int size = 256;
double sinTable[size];
#pragma omp parallel for
for(int n=0; n<size; ++n)
sinTable[n] = std::sin(2 * M_PI * n / size);
#pragma omp parallel for
for(int n=0; n<10; ++n)
{
printf(" %d", n);
}
printf(".\n");
// the table is now initialized
}
However, when I compiled, I failed:
dhcp-18-189-47-44:openmp_code myname$ gcc-4.8 tmp2.cpp
Undefined symbols for architecture x86_64:
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccFbBrPl.o
"std::ios_base::Init::~Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccFbBrPl.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
However, if I change it to g++-4.8, then it is successful...
dhcp-18-189-47-44:openmp_code myname$ g++-4.8 tmp2.cpp -fopenmp
I am wondering why this would happen....
You are compiling C++ code with 'gcc' (why?), so you need to link to it the standard c++ library. Add -stdc++ to the build command. When compiling with g++ it knows to link with this library automatically.
EDIT
On the other hand, your code has nothing related to C++ standard libraries. You have #include <iostream> but you use nothing from it. I think (didn't check it though) that if you comment out this include, your original build with 'gcc' should pass.
You're asking why your C++ program fails to compile with a C compiler, but compiles properly with a C++ compiler?
Then this rhetorical question is my answer.
The C compiler doesn't do things like linking in its C++ runtime. You need that runtime. Your linker errors show that the C++ runtime is not being linked. Because you're using a C compiler.

"clang: error: linker command failed with exit code 1" on "ld: symbol(s) not found for architecture x86_64"

Right, please bare with on this, it might be quite a long one, and one related issue was solved here (I think): CMake make[2]: *** No rule to make target `/path/to/uthash/utarray.h', needed by `HelloTest'. Stop.
I have been struggling for some days now to build a simple 'Hello World' programme which mixes C and C++, and pulls in various external libraries using CMake. For full disclosure it should be known that I am fairly new to C, C++ and CMake hence please be nice.
I am working in OS X Yosemite 10.10.4 (my googling seems to suggest this might be part of the problem). I am working out of the CLion IDE.
Alas, here we go, here is the programme I am trying to build:
#include <iostream>
#include "Simbody.h" \\Written in C++
extern "C"
{
#include "project_in_C.h"
}
using namespace std;
using namespace SimTK;
int main(int argc, char** argv) {
cout << "Hello, World!" << endl;
return 0;
}
We have a physics library written in C++ a bespoke project written in C, for which the main header file is, lets call it; project_in_C.h.
Again; all I am trying to do is build a simple mixed C/C++ project.
Now, the above is executed using CMake, and the following CMakeLists.txt file:
cmake_minimum_required(VERSION 3.2)
project(HelloTest)
# Simbody
find_package(Simbody REQUIRED)
include_directories(${Simbody_INCLUDE_DIR})
link_directories(${Simbody_LIB_DIR})
# Project C Headers
set(PROJC_ROOT_DIR "/Users/usr/project-c")
set(PROJC_INCLUDE_DIR ${PROJC_ROOT_DIR}/src
${PROJC_ROOT_DIR}/dir1
${PROJC_ROOT_DIR}/dir2)
include_directories(${PROJC_INCLUDE_DIR})
# Check that it has found the most important header
find_path(projFound project_in_C.h PATHS "/Users/usr/project-c/src")
if(NOT projFound)
message(FATAL_ERROR "Cannot find folder containing project_in_C.h")
endif()
# Project C Source [we want to avoid globbing]
set(PROJC_SOURCE ${PROJC_ROOT_DIR}/src/file1.c
${PROJC_ROOT_DIR}/src/file2.c
${PROJC_ROOT_DIR}/src/file3.c
${PROJC_ROOT_DIR}/src/file4.c
${PROJC_ROOT_DIR}/dir1/file5.c)
# Make library from source files
add_library(PROJC_LIBRARIES ${PROJC_SOURCE})
# Tie together
set(SOURCE_FILES main.cpp)
add_executable(HelloTest ${SOURCE_FILES})
target_link_libraries(HelloTest ${Simbody_LIBRARIES} ${PROJC_LIBRARIES})
So far so good, but here is where the truly mysterious problem arises. Upon build this is what I get in return:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/usr/Library/Caches/clion10/cmake/generated/c1d0f54d/c1d0f54d/Debug --target all -- -j 2
Scanning dependencies of target HelloTest
[ 91%] Built target PROJC_LIBRARIES
[100%] Building CXX object CMakeFiles/HelloTest.dir/main.cpp.o
Linking CXX executable HelloTest
Undefined symbols for architecture x86_64:
"_program_execution_wrapper", 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)
make[2]: *** [HelloTest] Error 1
make[1]: *** [CMakeFiles/HelloTest.dir/all] Error 2
make: *** [all] Error 2
But what the flippin' dynamite does that actually mean?
It is kicking up a fuss with project_in_C.h where the section the error seems to be referring to is written as so:
int program_execution_wrapper(int argc, char **argv);
int __program(int argc, char **argv);
#define main main(int argc, char **argv) { return program_execution_wrapper(argc, argv); } int __program
As ever, any help greatly appreciated.
Em...Undefined symbols is _program_execution_wrapper ... I don't see this symbol anywhere in you program. Is this maybe typo in your post? what if you change code in project_in_C.h from prog_exec_wrapper to program_execution_wrapper? And also, this function is not implemented in your code. For a start try something like this:
int program_execution_wrapper(int argc, char **argv) { printf("%s", "something"); }
Edit based on your comments:
See the following example:
We have two files (main.cpp)
#include <iostream>
int sum(int a, int b);
int main()
{
int a = sum(3, 5);
std::cout << a << std::endl;
return 0;
}
and file sum.cpp
#include <iostream>
int sum(int a, int b)
{
return a+b;
}
So I've I would like to compile this program...it would be like this
g++ main.cpp
I get the following error:
Undefined symbols for architecture x86_64:
"sum(int, int)", referenced from:
_main in main-22f955.o
ld: symbol(s) not found for architecture x86_64
But if I include sum.cpp in my main program, then program will work as charm.

os kern error : "ld: symbol(s) not found for architecture x86_64"

I have looked all over Stack Overflow and other websites about this famous error, and all of them are very specific, and in my case I cannot find a solution. I am making an ncurses application and when i try to compile it, it causes the following error:
Undefined symbols for architecture x86_64:
"NCRS::End()", referenced from:
_main in crspro-85eaaf.o
"NCRS::Start()", referenced from:
_main in crspro-85eaaf.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 compile the code with the following line:
$ clang++ crspro.cpp -lncurses -o crspro
Here is the code:
crspro.cpp
#include "ncrs.h"
int main(int argc, char* argv[]) {
NCRS::Start();
getch();
NCRS::End();
return 0;
}
ncrs.h
#ifndef NCRS_H
#define NCRS_H
#include <ncurses.h>
#include <string>
typedef std::string string;
class NCRS {
private:
static bool __curses_on;
static bool __buffer;
static bool __echo;
static bool __keypad;
public:
static void Start(bool bbuffer=false, bool becho=false, bool bkeypad=false);
static void End();
};
#endif
ncrs.cpp
#include "ncrs.h"
static void NCRS::Start(bool bbuffer=false, bool becho=false, bool bkeypad=false) {
initscr();
if (bbuffer) raw();
if (becho) echo(); else noecho();
if (bkeypad) keypad(stdscr, TRUE); else keypad(stdscr, FALSE);
__buffer = bbuffer;
__echo = becho;
__keypad = bkeypad;
__curses_on = true;
}
static void NCRS::End() { nocbreak(); echo(); keypad(stdscr, FALSE); endwin(); }
I don't have any issues in the code itself as far as I can tell. I have tried even including ncrs.cpp (The horror!!) but I still get the same problems.
Can anyone help with this issue? I've had this problem before with other projects and I've had to abandon them because I couldn't find a solution.
Thanks to anyone who can help!
_
_
EDIT
compile with:
clang++ crspro.cpp ncrs.cpp -lncurses -o crspro
returns error:
Undefined symbols for architecture x86_64:
"NCRS::__curses_on", referenced from:
NCRS::Start(bool, bool, bool) in ncrs-e52041.o
"NCRS::__echo", referenced from:
NCRS::Start(bool, bool, bool) in ncrs-e52041.o
"NCRS::__buffer", referenced from:
NCRS::Start(bool, bool, bool) in ncrs-e52041.o
"NCRS::__keypad", referenced from:
NCRS::Start(bool, bool, bool) in ncrs-e52041.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Your compilation isn't including anything from ncrs.cpp, which is where both NCRS::Start() and NCRS::End() are defined. You probably want
clang++ crspro.cpp ncrs.cpp -lncurses -o crspro
Or if you want to build the object files separately and then link them:
clang++ -c crspro.cpp -c
clang++ -c ncrs.cpp -c
clang++ crspro.o ncrs.o -lncurses -o crspro
Your next error about "NCRS::__curses_on" is because you're using static variables without defining them you need to add
bool NCRS::__curses_on=false;
bool NCRS::__buffer=false;
bool NCRS::__echo=false;
bool NCRS::__keypad=false;
to one of your .cpp files. (presumably ncrs.cpp is the logical place.)
It's probably worth thinking about whether they should be static (and whether the functions should be static too) - they may need to be, but static class variables are essentially global variables, which will often come back to bite you later. They make it harder to understand the flow of the code, and can make multi-threading and testing painful.