Mac mini m1 + VS Code + C Programming = Not Working - c

I am self learning to code in C using Mac mini M1 and VS Code. I get the below 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)
Please note:
Installed the VS Code Insiders for ARM apple version!
Clang is installed!
Xcode command line tools installed!
C/C++ 1.4.0-insiders2 extension is installed in VS Code!
Code Runner 0.11.4 is installed in VS Code!
Code:
#include <stdio.h>
int main()
{
printf("test");
return 0;
}
Compiled using:
gcc -Wall -Wextra -Werror -o test test.c

Okay! It's because you forget to save your program before executing i.e you should compile and run. Instead of just running it! #Keeplearning!

Related

Error on M1 Mac through GCC Compile in vim

I try to compile C files in vim environment but It occurs on error that
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)
My command is
gcc [filename.c] -o [filename]
and my file is
#include <unistd.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}
It is just simple code for checking compile.
But it didn't work.
So I wondering that what is the critical problem that occurs problem.
My command or M1 mac?
Of course I install gcc through 'HOMEBREW'.
Thank you.

Installing the Boehm GC on OS X

I want to install the Boehm garbage collector garbage collector on MacOS. I looked at this guide but it did not help; invoking brew install libgc did nothing. Here is my example code that I am trying to run:
#include <gc/gc.h>
int main() {
void* eight_bytes = GC_MALLOC(8);
}
Unfortunately, I get this error:
Undefined symbols for architecture x86_64:
"_GC_malloc", referenced from:
_main in boehm_invocation-369838.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 know of a good way to install this GC without building it from source?
When I installed libgc on mac, as you did, the files were installed to /usr/local/Cellar/bdw-gc/. Then, when it came time to compile my code I had to run:
$ LIBGC=/usr/local/Cellar/bdw-gc/VERSION/
$ gcc ... -I$LIBGC/include/ ... $LIBGC/lib/libgc.a other.a ...
When you install libgc, its not included in your system path. You need to explicitly add it.
Also in my code I used:
#include "gc.h"
And not <gc/gc.h>.

C program using SDL2 will not compile with clang

I am having an issue trying to compile my C program. I'm on macOS Catalina and using clang to compile.
When I try to SDL_Init( SDL_INIT_VIDEO ); and compile clang tells me there is a linker command failure
Undefined symbols for architecture x86_64:
"_SDL_Init", referenced from:
_main in main-1defaf.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 used brew to install SDL2 with brew install SDL2. So these are flags I have tried to have clang pass to the linker but neither one worked.
> clang main.c -L/usr/local/Cellar/sdl2/2.0.12_1/include/SDL2
> clang main.c -L/usr/local/Cellar/sdl2/2.0.12_1/include/SDL2 \
-L/usr/local/Cellar/sdl2/2.0.12_1/lib
Since they did not work I then tried to add the -framework flag. I followed the instructions I found here to install the framework. However the website didn't mention how to use it with clang. Clangs documentation did not mention if I needed to point to the framework file or directory so I tried a couple different ways but could not get any to work.
> clang main.c -L/usr/local/Cellar/sdl2/2.0.12_1/include/SDL2 \
-framework SDL2
ld: framework not found SDL2
clang: error: linker command failed with exit code 1 (use -v to see invocation)
> clang main.c -L/usr/local/Cellar/sdl2/2.0.12_1/include/SDL2 \
-framework /Library/Framework/SDL2.framework
ld: framework not found /Library/Framework/SDL2.framework
clang: error: linker command failed with exit code 1 (use -v to see invocation)
> clang main.c -L/usr/local/Cellar/sdl2/2.0.12_1/include/SDL2 \
-framework /Library/Frameworks/SDL2.framework/SDL2
ld: framework not found /Library/Frameworks/SDL2.framework/SDL2
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have also tried to just use XCode to compile and it builds successfully however fails to run with the error below. I followed the instructions in the tutorial linked above.
2020-06-30 16:07:36.220432-0700 sdl_test[1867:31873] Metal API Validation Enabled
2020-06-30 16:07:36.273590-0700 sdl_test[1867:32300] flock failed to lock maps file: errno = 35
2020-06-30 16:07:36.274022-0700 sdl_test[1867:32300] flock failed to lock maps file: errno = 35
Program ended with exit code: 0
I have restarted the computer and cleaned the build and based on some google searches it seems like this was introduced in a recent version of XCode that needs to be resolved.
I would like to stick with clang and not sure what else I can try to get this to compile. I would like to get some direction on next steps.
This is the code I am trying to run.
#include <SDL2/SDL.h>
#include <stdio.h>
int main(int argc, const char * argv[]) {
SDL_Init( SDL_INIT_VIDEO );
return 0;
}
You're missing the -lSDL2 to bring in the shared library, and if you're going to use /usr/local/Cellar/sdl2/2.0.12_1/include/SDL2 you should only #include <SDL.h>.
Consider using pkg-config to handle include and link flags.
clang main.c -o main `pkg-config --libs --cflags sdl2`

Compile Lua from source and create C module with it

I thought of compiling Lua from source code and then create a C module.
I compiled Lua with success but I can't build my C module.
So, I compiled Lua like this:
gcc -o Lua *.c -Os -std=c99
Compiled my module like this:
gcc -Wall -shared -fPIC -o module.so -I. module.c
But there are a few errors here:
Undefined symbols for architecture x86_64:
"_lua_pushcclosure", referenced from:
_luaopen_module in module-fb0b1f.o
"_lua_pushnumber", referenced from:
_super in module-fb0b1f.o
"_lua_setglobal", referenced from:
_luaopen_module in module-fb0b1f.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 C module itself:
#include "lua.h"
static int super(lua_State* L) {
lua_pushnumber(L, 5);
return 1;
}
int luaopen_module(lua_State* L) {
lua_register(L, "super", super);
return 0;
}
My Lua script:
require("module")
print(super())
I'm on Unix based system (Mac), but I want it to work on Linux as well.
Edit:
Problem to compile C module was fixed by entering -bundle -undefined dynamic_lookup instead of -shared (Thanks lhf).
But I can't import the module in Lua.
> require("module")
error loading module 'module' from file './module.so':
dynamic libraries not enabled; check your Lua installation
Another thing:
This seems to be a quick fix only; -bundle -undefined dynamic_lookup. This does not work on Linux. How can I do this on linux? I wanted a solution for Unix based systems.
Download Lua from lua.org and build Lua with make macosx. See Getting started.
Use -bundle -undefined dynamic_lookup instead of -shared to build module.so.
Use require"module" to load it into Lua.
Call super.
Make sure you're running the lua program that you have built above, not some other version that is installed.

Macosx, C and embedded lua

Ok, what we have:
Program written on C which compiling and running without problems in Linux and MacOSX (leopard).
I embedded lua code today. Linux: compiled lua 5.1 from source. Everything works and compiles without any problems. Macos: compiled the same lua 5.1 package.
Linked with --llua.
Started compile and got an error:
CC=gcc-4.0 make
ld: warning: in /usr/local/lib/liblua.a, file is not of required architecture
Also tried reinstall, complete remove and installing from macports. The same.
So is there any fix for that?
The error "file is not of required architecture" hints to the fact that you're trying to mix architectures.
Check the architecture of the /usr/local/lib/liblua.a and make sure it matches the architecture or the object you're trying to build.
E.g.
We have a i386 object:
==== cat fun.c ====
#include <stdio.h>
void fun()
{
printf("%s", "foobar\n");
}
gcc -arch i386 -c fun.c -o fun.o
if we try to use it when compiling a x86_64 object (default architecture in Mac OS X):
===== cat test.c ==
extern void fun();
int main()
{
fun();
}
we get:
$ gcc test.c fun.o
ld: warning: ignoring file fun.o, file was built for i386 which is not the architecture being linked (x86_64)
Undefined symbols for architecture x86_64:
"_fun", referenced from:
_main in ccXVCQhG.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Resources