VS code Undefined symbols for architecture arm64 - c++

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)

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.

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

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.

Undefined symbols building in C++ on macOS

While implementing class member functions in.cpp file on macOS 10.15, displaying an error:
clang: Undefined symbols for architecture x86_64:error:
"Circle::Area()", referenced from:
_main in main-4cfa92.o
"Circle::Circle(double)", referenced from:
_main in main-4cfa92.o
linker command failed with exit code 1 (use -v to see invocation)
To verrify this case, I found an example on the website to try, and it worked fine when I copied the function definitions from Circle.cpp file into Circle.h file, But when the function declaration is in Circle.h and function definition is in Circle.cpp files, respectively, an error occurs
//Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle
{
private:
double r;//radius
public:
Circle();//constructor
Circle(double R);//The constructor
double Area();//computing area
};
#endif
//Circle.cpp
#include "Circle.h"
Circle::Circle(){
this->r=5.0;
}
Circle::Circle(double R){
this->r=R;
}
double Circle::Area(){
return 3.14*r*r;
}
//main.cpp
#include <iostream>
#include "Circle.h"
using namespace std;
int main(){
Circle c(3);
cout<<"Area="<<c.Area()<<endl;
return 0;
}
Error message:
Undefined symbols for architecture x86_64:
"Circle::Area()", referenced from:
_main in main-4cfa92.o
"Circle::Circle(double)", referenced from:
_main in main-4cfa92.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Looks like you have not included Circle.cpp in the compilation step. Make sure you include both main.cpp and Circle.cpp

"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.

Undefined reference when using Cmake and QtCreator

I'm running the basic OpenCV example with OpenCV3.0.0 dev:
project(ImageDenoise)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
MESSAGE(${OpenCV_LIBS})
MESSAGE(${OpenCV_INCLUDE_DIRS})
Source code:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main(int argc, char* argv[] )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], IMREAD_COLOR );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
When I import this project into QtCreator, I got the following linking errors when building project:
[100%] Building CXX object CMakeFiles/ImageDenoise.dir/main.cpp.o
Linking CXX executable ImageDenoise
CMakeFiles/ImageDenoise.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x7c): undefined reference to `cv::imread(cv::String const&, int)'
main.cpp:(.text+0xf5): undefined reference to `cv::namedWindow(cv::String const&, int)'
main.cpp:(.text+0x144): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
However when I run cmake from command line and using make, then it works perfectly. What is the reason behind this?
dzung#Cronus:~/kSVD/build$ make
Scanning dependencies of target ImageDenoise
[100%] Building CXX object CMakeFiles/ImageDenoise.dir/main.cpp.o
Linking CXX executable ImageDenoise
[100%] Built target ImageDenoise
dzung#Cronus:~/kSVD/build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake ImageDenoise Makefile
dzung#Cronus:~/kSVD/build$ ./ImageDenoise
usage: DisplayImage.out <Image_Path>
I had exactly the same issue. I think this is some sort of a Qt-creator bug.
I solved this by:
Remove everything except *.cpp and CMakeLists.txt in the project
folder.
Before creating anything with Qt-creator do: cmake . && make
Now open existing project in Qt-creator.
Now you can run cmake/compile/run etc just fine.