Undefined symbols for architecture x86_64 in C - c

Today I installed the Allegro game programming library for C and I’ve tried to include one of the header files but when I try to execute gcc -I./include example.c -o a.exe in the terminal, I keep on getting this error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
(maybe you meant: __al_mangled_main)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any ideas? I installed Allegro 5 using the instructions here: https://wiki.allegro.cc/index.php?title=Install_Allegro5_From_GIT/OSX
example.c code:
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, const char *argv[]){
puts(“Hello, world!”);
return 0;
}

You need to link your executable to Allegro.
According to the FAQ, you should add -lallegro to your compile command, or -lallegro -lallegro_main on OSX
You may need other flags, and Allegro 5 uses pkg-config instead of allegro-config, so do pkg-config allegro-5.0 allegro_main-5.0 --cflags --libs to find out.
You can combine this into a compiler command by using backticks, e.g.
$CC -W -Wall `pkg-config allegro-5.0 allegro_main-5.0 --cflags --libs` foo.c -o foo.exe

Related

How do I include the object files for openssl/md5 library?

I have included the openssl/md5.h in my source.
#include <openssl/md5.h>
This is my (part of) my code:
char *pf_generate_pfdhr_string(int firstfree, int numpages)
{
const int md5_digest_len = 16;
char hash[md5_digest_len];
MD5_CTX md5_ctx;
MD5_Init(&md5_ctx);
int hdr_arr[2] = {firstfree, numpages};
MD5_Update(&md5_ctx, hdr_arr, 2*sizeof(int));
MD5_Final(hash, &md5_ctx);
return hash;
}
And this is my output
ranlib build/libpf.a
cc -g -O2 -Wall -Wextra -Isrc -rdynamic tests/pf_tests.c build/libpf.a -o tests/pf_tests
Undefined symbols for architecture x86_64:
"_MD5_Final", referenced from:
_pf_generate_pfdhr_string in libpf.a(pf.o)
"_MD5_Init", referenced from:
_pf_generate_pfdhr_string in libpf.a(pf.o)
"_MD5_Update", referenced from:
_pf_generate_pfdhr_string in libpf.a(pf.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: *** [tests/pf_tests] Error 1
I have included the header file so the compiler knows the function exists. However, it seems unable to find the object files for the md5 library. How can I include it in my build?
If I am using Make, what is the best way to do so?
Thank you.
Check the documentation for your platform. It may be -lcrypto -lssl. It may not be. If your platform supports pkg-config, use pkg-config --libs openssl.

How to compile gcc 32bit app on 64bit osx

i try to compile a 32 bit app on my mac osx 64bit
I have a 32bit lib included.
I try to create a personal lib
gcc -m32 -c fileA.c -Iinclude -o fileA.o
gcc -m32 -c fileB.c -Iinclude -o fileB.o
All Ok
now I create .a File
ar ruv ./lib/myLib.a fileA.o fileB.o
When I try to launch
gcc -m32 -o imageMod imageMod.c -Iinclude -Llib
I receive the following error
Undefined symbols for architecture i386: "_addozzo", referenced
from:
_main in imageMod-nfyyGP.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1
(use -v to see invocation)
anyone can help me?
You need to tell gcc to link in the library:
gcc -m32 -o imageMod imageMod.c -Iinclude -Llib -l:myLib.a

Eclipse build configuration for OpenMP

I am trying to learn OpenMP, starting with the following simple snippet
#include <stdio.h>
#include <stdlib.h>
int main(void) {
#pragma omp parallel
printf("Hello OpenMP!\n");
return 0;
}
Simply compiling from the command line works:
cls ~/Desktop $ gcc -fopenmp HelloOpenMP.c -o HelloOpenMP
cls ~/Desktop $ ./HelloOpenMP
Hello OpenMP!
Hello OpenMP!
However, I'd like to use Eclipse with CDT. I created a new build configuration "OpenMP" and tried to add the -fopenmp flag under "Miscellaneous", copying the other settings from the "Debug" build configuration.
The build fails with
14:56:16 **** Incremental Build of configuration OpenMP for project HelloOpenMP ****
make all
Building file: ../src/HelloOpenMP.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -fopenmp -MMD -MP -MF"src/HelloOpenMP.d" -MT"src/HelloOpenMP.d" -o "src/HelloOpenMP.o" "../src/HelloOpenMP.c"
Finished building: ../src/HelloOpenMP.c
Building target: HelloOpenMP
Invoking: MacOS X C Linker
gcc -o "HelloOpenMP" ./src/HelloOpenMP.o
Undefined symbols for architecture x86_64:
"_GOMP_parallel_end", referenced from:
_main in HelloOpenMP.o
"_GOMP_parallel_start", referenced from:
_main in HelloOpenMP.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [HelloOpenMP] Error 1
So I guess this was not the right place to add the -fopenmp compiler option? What configuration should I use to build with OpenMP?
Add -fopenmp flag to the linker section as well.

Undefined symbol "start" while linking D program through LD

I have following simple program:
import std.stdio;
int main(string[] argv) {
writeln("Hello, world!");
return 0;
}
I'm building it as follows:
DMD -c -m64 -od/proj/out -w -wi -fPIC -debug \
-g -I/proj/hello -unittest /proj/hello.d
LD -L/usr/share/dmd/lib/ -arch x86_64 -execute -macosx_version_min 10.7 \
-pie -lm -lpthread -lphobos2 -o /proj/out/hello_app /proj/out/hello.o
Compilation passes perfectly, but linking stucks with following:
Undefined symbols for architecture x86_64:
"start", referenced from:
-u command line option
(maybe you meant: _D3std9algorithm41__T10startsWithVAyaa6_61203d3d2062TAhTAhZ10startsWithFAhAhZb, _D4core6thread6Thread5startMFZv , _D3std9algorithm91__T10startsWithVAyaa11_62203c20612e74696d6554TAS3std8datetime13PosixTimeZone10TransitionTlZ10startsWithFAS3std8datetime13PosixTimeZone10TransitionlZb , _D3std9algorithm43__T10startsWithVAyaa6_61203d3d2062TAyaTAyaZ10startsWithFAyaAyaZb , _D3std9algorithm41__T10startsWithVAyaa6_61203d3d2062TAxaTaZ10startsWithFAxaaZb , _D3std9algorithm92__T10startsWithVAyaa11_62203c20612e74696d6554TAS3std8datetime13PosixTimeZone10LeapSecondTylZ10startsWithFAS3std8datetime13PosixTimeZone10LeapSecondylZb , _D3std9algorithm92__T10startsWithVAyaa11_62203c20612e74696d6554TAS3std8datetime13PosixTimeZone10TransitionTylZ10startsWithFAS3std8datetime13PosixTimeZone10TransitionylZb )
ld: symbol(s) not found for architecture x86_64
I guess I forgot some additional static library to link with to have it setup everything, but what exactly?
Also I've seen instructions about how to do separate compilation and linking somewhere on dlang site, but cannot find it.
UPD1: When linking with help of GCC using gcc -L/usr/share/dmd/lib/ -lphobos2 -lm -lpthread hello.o, it works, but I need to use ld.
Add -lcrt1.o when linking.
LD -L/usr/share/dmd/lib/ -arch x86_64 -execute -macosx_version_min 10.7 \
-pie -lm -lpthread -lphobos2 -lcrt1.o -o /proj/out/hello_app /proj/out/hello.o
[update]
Ah, you got it : )
Found it due to pure luck!
It should be linked with -lphobos2 -lm -lpthread and -lcrt1.o - then everything links and works fine.

Missing symbol compile error in C. Basic header file setup

I'm working on a C project implementing some generic containers and am having this weird issue when compiling. Here is some sample code that also replicates the error.
foo.h
void fooprint(void);
foo.c
#include "foo.h"
#include <stdio>
void fooprint(void){
printf("bar");
return;
}
main.c
#include "foo.h"
int main(void){
fooprint();
return 0;
}
I compile by typing
gcc main.c -o main
and this is what terminal outputs
Undefined symbols:
"_fooprint", referenced from:
_main in ccfMXGzj.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
I'm compiling this on an OSX system but have also tried compiling on a red hat machine with the same effect.
The solution is probably painfully obvious but I have had multiple friends I'm working with look at this and they couldnt see the problem. I've googled around a lot but most symbol error issues are usually pertaining to objective C.
You need to compile them together:
gcc -Wall -Wextra -o main main.c foo.c
Or maybe make a Makefile ?
all: main
main: main.o foo.o
main.o: main.c
foo.o: foo.c
You have to compile also foo.c into an object file and link all of them together:
gcc -o foo.o foo.c
gcc -o main.o main.c
gcc -o main main.o foo.o
Yes, this is simple, so I recommend you to read a good C book and step these easy steps.

Resources