C program using SDL2 will not compile with clang - c

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`

Related

Is it posible to compile Flex/Lex on a M1 Mac

In my compilers class we are writting Flex/Lex code. When I compiled the .l file and tried to compile the resultant lex.yy.c file with gcc, I got the following error:
Undefined symbols for architecture arm64:
"_yywrap", referenced from:
_yylex in lex-fb85c9.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Does is posible to compile it in a Apple Silicon (M1) Mac, at least in a Linux VM?
I created a test file and compiled:
% flex lex.l
% cc -o lex lex.yy.c -lc -ll
% grep 'yylex();' lex.yy.c
yylex();
The -ll on the cc command links against the libl library. Current M1 based macOS does not provide a libfl library you may see referenced.

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

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!

a contradictory result when checking an example of "Linking with external libraries"

I just started to learn gcc/g++ by reading "An introduction to GCC"
In chapter 3.1 the author showed an example of linking with external libraries and header files. The main source code uses 'gdbm.h' header (installed at /opt/gdbm-1.8.3/include/ in text book) file and the library ‘libgdbm.a’ (installed at /opt/gdbm-1.8.3/lib/ in text book). The process of linking with the library and header file is as follows:
Link to example screenshot
When I tried to repeat this, I got a contradictory result. When I used method 1 (in the example fails) which only includes header file path, the program can be compiled, however, when I used method 2 (in the example succeeds), I got an error, saying that
"ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1"
(this is my full command line and error message:)
userMacBook-Pro:03 user$gcc -Wall -I/usr/local/include dbmain.c -L/usr/local/lib dbmain.c -lgdbm
duplicate symbol _main in:
/var/folders/6_/09vfzzms7dq1d73vl4mwlxmh0000gn/T/dbmain-9d15f9.o
/var/folders/6_/09vfzzms7dq1d73vl4mwlxmh0000gn/T/dbmain-907e96.o
this is my command that succeeds:
userMacBook-Pro:03 user$ gcc -Wall -I/usr/local/include dbmain.c -lgdbm
userMacBook-Pro:03 user$ ./a.out
Storing key-value pair... done.
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Can anyone explain why this happens?
I don't know which platform the author uses, I just use mac OS X.
gcc -Wall -I/usr/local/include dbmain.c -L/usr/local/lib dbmain.c -lgdbm
The file dbmain.c is present twice on your command line. For this reason the main function is present twice.

Linking fails with gcc 4.8.2 / ld 2.24, succeeds with gcc 4.4.7 / ld 2.20

In a chroot based on CentOS 6.4 I'm working in, linking against ncurses with ld 2.20 succeeds, but linking with ld 2.24 fails. I don't directly invoke the linker, gcc is handling it -- gcc 4.4.7 is using ld 2.20, and gcc 4.8.2 is using ld 2.24.
Here is a minimal example that fails to link with gcc 4.8.2 / ld 2.24 in my particular environment.
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
WINDOW* window = NULL;
if (!(window = initscr())) {
printf("Error initializing ncurses.");
exit(1);
}
halfdelay(50);
getch();
endwin();
}
Success (ld 2.20):
$ gcc main.c -lncurses -Wl,--verbose | grep "ncurses.*succeeded"
attempt to open /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/libncurses.so succeeded
$
Failure (ld 2.24):
$ /opt/gcc/4.8.2/bin/gcc48 main.c -lncurses -Wl,--verbose | grep "ncurses.*succeeded"
attempt to open /usr/lib/../lib64/libncurses.so succeeded
/opt/binutils/2.24/bin/ld24: /tmp/ccCxUFxl.o: undefined reference to symbol 'halfdelay'
/lib64/libtinfo.so.5: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
$
Note that both commands appear to link against the same libncurses.so.
For what it's worth, in a different chroot based on CentOS 5.4, there is a different libncurses version, and it links fine with ld 2.24, but unfortunately, building in that chroot is not an option. The nm utility shows that the (static) libncurses.a here does have the required symbols (nm lists no symbols for libncurses.so -- so I'm just assuming they are similar).
In the CentOS 6.4 chroot, however, nm shows that all of the ncurses symbols I'm getting "undefined reference" messages for are indeed undefined or not present in libncurses.a in the Centos 6.4 chroot, which is confusing, because linking with gcc 4.4.7 works. Something is not right.
Also, I tried producing an object with gcc 4.4.7, and then linking with gcc 4.8.2, but that did not help.
I'm confused as to why the one compiler / linker would succeed while the other would fail. Is this an ABI issue? Does anyone know what is happening here? Are there any flags I can pass to gcc to make the new linker work?
Your libncurses library is itself linked to libtinfo, which causes your older toolchain to also look for symbols in libtinfo.
But newer toolchains normally runs the linker with the --as-needed, and the --no-copy-dt-needed-entries, the latter which probably causes the difference you're seeing.
Basically you'll need to link to libtinfo as well, which is where the halfdelay function resides.
gcc main.c -lncurses -ltinfo

Xcode 5.1.1 build fail C programming

I am trying to build and run a basic C program in Xcode, but keep getting a "build failed" message. The script is nothing too crazy, just a natural number calculator, there's nothing wrong with the script as I've tested it on an online compiler and it works fine.
This is the full error message:
Ld /Users/Matt/Library/Developer/Xcode/DerivedData/testing.c-ckojksceiqxrcdcfavsxvsgvapin
/Build/Products/Debug/testing.c normal x86_64
cd /Users/Matt/Code/testing.c
export MACOSX_DEPLOYMENT_TARGET=10.9
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform
/Developer/SDKs/MacOSX10.9.sdk -L/Users/Matt/Library/Developer/Xcode/DerivedData/testing.c-
ckojksceiqxrcdcfavsxvsgvapin/Build/Products/Debug -F/Users/Matt/Library/Developer/Xcode/DerivedData
/testing.c-ckojksceiqxrcdcfavsxvsgvapin/Build/Products/Debug -filelist /Users/Matt/Library/Developer
/Xcode/DerivedData/testing.c-ckojksceiqxrcdcfavsxvsgvapin/Build/Intermediates/testing.c.build/Debug
/testing.c.build/Objects-normal/x86_64/testing.c.LinkFileList -mmacosx-version-min=10.9 -Xlinker
-dependency_info -Xlinker /Users/Matt/Library/Developer/Xcode/DerivedData/testing.c-
ckojksceiqxrcdcfavsxvsgvapin/Build/Intermediates/testing.c.build/Debug/testing.c.build/Objects-
normal/x86_64/testing.c_dependency_info.dat -o /Users/Matt/Library/Developer/Xcode/DerivedData
/testing.c-ckojksceiqxrcdcfavsxvsgvapin/Build/Products/Debug/testing.c
duplicate symbol _main in:
/Users/Matt/Library/Developer/Xcode/DerivedData/testing.c-ckojksceiqxrcdcfavsxvsgvapin/Build
/Intermediates/testing.c.build/Debug/testing.c.build/Objects-normal/x86_64/tests.o
/Users/Matt/Library/Developer/Xcode/DerivedData/testing.c-ckojksceiqxrcdcfavsxvsgvapin/Build
/Intermediates/testing.c.build/Debug/testing.c.build/Objects-normal/x86_64/main.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Anybody know how to solve this? I've been searching for an answer for ages with no luck.
You have two "main" functions defined.
One in a file named "main.c" and the other in a "tests.c" file. Remove one of them and you should be okay!
Or, if you only have a "testing.c" file, do a Clean on your xcode project and try building again.

Resources