VS Code extension coderunner not working properly on macOS - c++

I am new to programming and wanted to setup c++ on my MacBook Air M1 running on macOS Monterey. I followed some online tutorials to download Homebrew, Mingw-64(using Homebrew) and finally installed VS Code and then installed the c++ extension pack and code-runner extension.
However when I tried to run the following code:
#include <iostream>
using namespace std;
int main(){
cout << "helloworld" << endl;
return 0;
}
I got the following error:
Undefined symbols for architecture arm64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Can somebody please help me out?

Related

Using armadillo in atom with script

I there a way to compile a c++ file using script in atom that uses armadillo? I couldn't find anything related to it.
I already installed armadillo and tried compiling some basic code using script in atom:
#include <iostream>
#include <armadillo>
int main(int argc, const char **argv) {
arma::arma_rng::set_seed_random();
arma::Mat<double> A = arma::randu(4,4);
std::cout << "A:\n" << A << "\n";
return 0;
}
This is the error I got:
Undefined symbols for architecture x86_64:
"thread-local wrapper routine for arma::arma_rng_cxx11_instance", referenced from:
arma::arma_rng::set_seed(unsigned long long) in test1-83e853.o
arma::arma_rng::randu<double>::fill(double*, unsigned long long) in test1-83e853.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I read the source code for this atom-script thing correctly, it just hardcodes -Wall -include stdio.h as compilation options. You need to add -larmadillo to link it to the Armadillo library.
In short: you cannot use atom-script in combination with custom libraries, unless you edit the extension to hardcode the libraries in your compilation commandline.

ld: symbol(s) not found for architecture x86_64 clang: error:

This is for a Mac using Eclipse
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello, world!!!" << endl;
return 0;
}
Here is the compiler output:
Invoking: MacOS X C++ Linker g++ -o "Lab2" ./secondlab.o
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: *** [Lab2] Error 1
All you need to do is configure the C++ Linker correctly under CDT/eclipse one has to enter "sndfile" in the text field found under:
Project->Properties->C/C++ Build->Settings->Mac OS C++
Linker->Libraries->"Libraries -l"
The "-l" gets added to "sndfile" automatically to create "-lsndfile"
It is also important however to point the Compiler to the correct include directory (containing "sndfile.h"). I guess most installations would install this into "/usr/local/include".
For a C++ project in CDT/eclipse, this would need to be entered in
Project->Properties->C/C++ Build->Settings->GCC C++
Compiler->Includes->"Include paths (-l)"
And please do take a look at this thread.

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.

Setting up GMP Library on XCode for C++

I had a question on setting up GMP Library to work on XCode for C++ programming. Although I have tried pursuing the GMP manual and also other similar stack questions on getting the library to work properly, I still have not been able to properly get the library to work. I am new to C++ and programming in general so please bear with me.
Things I have tried:
Install GMP library through homebrew using brew install gmp
Using the XCode interface under header search paths and library search paths added the path "/usr/local/include" (this is where homebrew installs the gmp library)
Under other linker flags added -lgmp -lgmpxx
Tried running various sample codes e.g.
#include <iostream>
#include <stdio.h>
#include <gmp.h>
#include <gmpxx.h>
using namespace std;
int main ()
{
mpz_t p;
mpz_init(p);
return 0;
}
However this code fails to compile and the XCode debugger has the following error messages:
Undefined symbols for architecture x86_64:
"___gmpz_init", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Thanks!

ld: symbol(s) not found for architecture

First of all I want to say I realise this has been asked a dozen times, and I've read countless solutions without any luck so therefore I'm asking again.
I'm using OS X Mavericks 10.9.5, writing in Sublime 3 and compiling using terminal with g++
I'm trying to compile this simple file (test.cpp)
#include <iostream>
#include <SDL2/SDL.h>
int main () {
if(SDL_Init(SDL_INIT_VIDEO)) {
std::cout << "I made it this far" << std::endl;
}
return 0;
}
Compiler line :
g++ test.cpp
This returns the error :
Undefined symbols for architecture x86_64:
"_SDL_Init", referenced from:
_main in test-ebbae4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So, I've tried adding a lot of different flags, -m32 only changes the result to throw Undefined symbols for architecture i386: and ld: symbol(s) not found for architecture i386
I've tried different -arch variations, but I can't seem to get it to work. also played around with -l -I flags but I'm not sure I know what they do/how they could help..
Does anyone have a clue what to do next?
EDIT : Some additional information. I've tried using the .framework for SDL2, that didn't change anything, currently I'm using SDL2 installed through compilation of the source. Headers located in usr/local/SDL2
g++ test.cpp
You should specify the SDL library, too:
g++ test.cpp -lsdl2
You might need to include a path if its not well known to the compiler driver:
g++ test.cpp -L/path/to/the/library -lsdl2