Undefined symbols for architecture arm for - c++

I am new to C++, and trying to use get_string, but I am not sure what I writing wrong that is creating an error.
The code I have is the following:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
string name = get_string("What's your name? ");
printf("hello, %s\n", name);
}
and it keeps saying the following error:
Undefined symbols for architecture arm64:
"_get_string", referenced from:
_main in hello-890d43.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [hello] Error 1
Does any one know what I am doing wrong?
I expected the code take an input and print out hello, (your input).

The same thing happened to me when trying to use the cs50 C library on my local Mac with M1 chip.
Even after installing the library as described here: https://cs50.readthedocs.io/libraries/cs50/c/
It still didn't work.
The problem is coming from the fact that the make command here doesn't include the link command by default.
Assuming your source file is named hello.c instead of just doing:
make hello
You have to enter the following in your terminal in order to compile:
clang hello.c -o hello -lcs50
Afterwards, when running the executable, it works as expected:
./hello

make hello LDLIBS="-lcs50"
LDLIBS to include the cs50 library

Related

C++ files stopped compiling - keep getting error: linker command failed with exit code 1 (use -v to see invocation)

I've had no problems compiling in the past, and code that I wrote previously is compiling fine, but it is now giving this error when I try to compile a very simple C++ file using Visual Studio Code on my M1 Mac:
Code:
#include<iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 0;
}
The terminal commands I've tried (all give same error):
g++ -std=c++14 test.cpp -o test
g++ -std=c++11 test.cpp -o test
g++ test.cpp -o test
Error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
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 tried to delete and re-create the file with different names, restarting VSC, using different output file names and nothing changes this.
This was occurring as I hadn't saved the file prior to compiling and therefore the compiler couldn't find it.

How to use the fmt library without getting "Undefined symbols for architecture x86_64"

I'm trying to use the fmt (https://github.com/fmtlib/fmt) formatting header library in my c++ project.
I've added the path to the core header file at the top of my main file like so:
#include "../third_party/fmt/core.h"
but when I try to call any function like:
string message = fmt::format("The answer is {}", 42);
I get the following error:
Undefined symbols for architecture x86_64:
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > fmt::v5::internal::vformat<char>(fmt::v5::basic_string_view<char>, fmt::v5::basic_format_args<fmt::v5::buffer_context<char>::type>)", referenced from:
std::__1::basic_string<std::__1::enable_if<internal::is_string<char [17]>::value, fmt::v5::internal::char_t<char [17]>::type>::type, std::__1::char_traits<std::__1::enable_if<internal::is_string<char [17]>::value, fmt::v5::internal::char_t<char [17]>::type>::type>, std::__1::allocator<std::__1::enable_if<internal::is_string<char [17]>::value, fmt::v5::internal::char_t<char [17]>::type>::type> > fmt::v5::format<char [17], int>(char const (&) [17], int const&) 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]: *** [main] Error 1
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2
I'm not sure how to use this as this is how I have used other header libraries in the past such as cxxopts. Any help would be appreciated!
You should link with the fmt library or use the optional header-only mode.
For example, if you have the file test.cc:
#include <fmt/core.h>
int main() {
fmt::print("The answer is {}.", 42);
}
You can compile and link it with gcc:
g++ -std=c++11 test.cc -lfmt
From a comment in #vitaut's answer, if you change your #include line from this:
#include "../third_party/fmt/core.h"
to this:
#include "../third_party/fmt/format.h"
it will cause the code to be compiled in "header-only mode", and you won't need to change your build process to compile and link in the {fmt} library.
I'm working on Mac, and I did not realize that you can install the library using brew. It appears at the end of the page. I have been dealing with symbol errors all evening, and I'm not sure that all my problems were related to the build process. The compiling process was also not working properly.
The paths where the library is installed are: /usr/local/include and /usr/local/lib.
I'm using g++-11 to build my project and this instruction works for me:
g++-11 -std=c++20 -I/usr/local/include -L/usr/local/lib -lfmt main.cpp -o main
The only problem is that it works partially. It works fine with print:
fmt::print("Don't {}!\n", "panic");
But it breaks using format:
fmt::format("Don't {}!\n", "panic");
I'm missing something, but I'm not sure what.
By the way, if you are using VSCode, you can create a c_cpp_properties.json into your .vscode folder and add the include path for the headers.
{
"includePath": [
[...],
"/usr/local/include/"
],
}
Not sure if this is related to your case, but I hope it helps.

vscode g++ Link failure : Undefined symbols for architecture x86_64

Basic Info:
system: macOS High Sierra(10.13.6)
editor : vs code(latest version)
Compiler: g++ (Xcode)
Target:deploy GLFW + GLAD
Question Description:
Recently, I'm learning to do some Computer Graphics related work. Everything is going smooth. However, when i create a window to test the env.Link error happened:
Undefined symbols for architecture x86_64:
"_gladLoadGLLoader", referenced from:
_main in main-5c211c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The terminal process terminated with exit code: 1
It seems I have not link some third party file. I
have fixed exactly the same problem by add "-lglfw" args to g++ when using functions in glfw3.h.
But when meeting glad related function : gladLoadGLLoader, I don't know how to do.
Something I have done:
Can find the head file.
#include glad/glad.h
#include GLFW/glfw3.h
Have put the file "glad.c" in workspace.
Try to Add "g++ -framework XXXXX" , But doesn't work.
Try to Add "g++ -lglfw3", But doesn't work.
Add "g++ -L or I /usr/lib or /usr/local/lib or /usr/local/include", But doesn't work.
Just tell the g++ to compile glad.c by adding "glad.c" to args. I thought glad.c will be compiled by default. Although I am not clear what happened, the problem is resolved anyway.
Add glad.c into Build Phases->Compile Sources

libuvc program does not compile

I'm trying to use libuvc in one of my C/C++ projects. I succesfully compiled and installed both libusb and libuvc from source and when I attempt to compile the following code using gcc:
#include "libuvc/libuvc.h"
int main (int argc, const char **argv) {
uvc_init(NULL, NULL);
}
I get the following error:
Undefined symbols for architecture x86_64:
"_uvc_init", referenced from:
_main in main-de2855.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'm on MacOS High Sierra 10.13.1 x64.
I used the command:
gcc main.c -luvc -o main
and it worked! Adding the -luvc flag solved my problem, but I don't understand why I need to use it. I've never needed flags before when working with other C/C++ dependencies.

MacOSx - Compiling a C project using make command

I am working on an assignment for my System Programming course which is a course using the linux terminal and C language. I own a mac and already require a windows VM for my other courses so I have no room for the linux machine and my professor said that my mac machine would work fine as it has the linux terminal.
We have to create a makefile file that will compile and link the c project together and create a .bin file, which is the basis of programming C in a linux environment, but I am getting errors with my makefile.
I believe it has to do with the difference between Linux and MacOS but I am not sure.
My makefile is as follows:
./bin/cryptoMagic: ./obj/main.o
cc ./obj/main.o -o ./bin/cryptoMagic
./obj/main.o: ./src/main.c ./inc/prototypes.h
cc ./src/main.c -c -o ./obj/main.o
When I run the make utility in the terminal I get this error message:
kyles-MacBook-Pro:~ KyleJensen$ cd Documents/School/First\ Year/Semester2/SystemProgramming/Assignments/Assign01/
kyles-MacBook-Pro:Assign01 KyleJensen$ make
cc ./obj/main.o -o ./bin/cryptoMagic
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [bin/cryptoMagic] Error 1
Silly me. I didn't have a valid main function in my C file. Watch out for this one in the future kids!