On Apple M1, I'm trying to compile the following code:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
inline uint32_t rotl32(uint32_t x, int32_t bits)
{
return x<<bits | x>>(32-bits); // C idiom
}
uint32_t bad_hash32(char const *input) {
uint32_t result = 0xC0FF117;
while (*input) {
result ^= *input++;
result = rotl32(result, 5);
}
return result;
}
int main(int argc, char* argv[])
{
char const* const input = argv[1];
printf("%08x\n", bad_hash32(input));
return 0;
}
Command:
gcc bad_hash.c -o bad_hash
It produces the following error:
Undefined symbols for architecture arm64:
"_rotl32", referenced from:
_bad_hash32 in bad_hash-4c8a24.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What's the issue here? I tried to upgrade gcc to the latest.
Clang version:
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.2.0
Thread model: posix
Removing the inline specifier fixes the issue.
Though I still don't know why this happens.
Related
This question already has answers here:
undefined symbol for architecture x86_64 in compiling C program [duplicate]
(1 answer)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
Why does main.c fail to compile when including sum_divisibles.h?
I am sure I am missing something silly, but can't find it for the life of me.
Compiling on Mac OSX.
sum_divisibles.h
#ifndef SUM_DIVISIBLES
#define SUM_DIVISIBLES
int sum_divisibles(int limit);
#endif /* SUM_DIVISIBLES */
sum_divisibles.c
#include <stdlib.h>
#include "sum_divisibles.h"
int sum_divisibles(int limit)
/* Returns sum of multiples of 3 and 5 less than limit. */
{
int mult_3, mult_5, sum = 0;
for (int ndx=0; (mult_3 = ndx * 3 ) < limit; ndx++) {
if ( (mult_5 = ndx * 5) < limit && mult_5 % 3 != 0 ) {
sum += mult_5 ;
}
}
return sum; }
main.c
#include <stdio.h>
#include "sum_divisibles.h" // does not compile
//#include "sum_divisibles.c" // compiles
int main(void)
{
printf("%d\n", sum_divisibles(1000));
}
Error -- on gcc -Wall main.c
Undefined symbols for architecture x86_64:
"_sum_divisibles", referenced from:
_main in main-e4e97
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am unable to compile to c program for 32bit machine from 64bit linux machine using command gcc -m32 -Werror a.c -o a
It shows me the error
In file included from a.c:1:
/usr/include/stdio.h:27:10: fatal error: bits/libc-header-start.h: No such file or directory
#include <bits/libc-header-start.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
I check the stdio.h in /usr/include/stdio.h
here in my machineLinux kali 4.19.0-kali4-amd64 #1 SMP Debian 4.19.28-2kali1 (2019-03-18) x86_64 GNU/Linux the line #include <bits/libc-header-start.h> is included while in other ubuntu 64 bit this line is not included
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int functionFunction(char *param)
{
char *localString = "Conjunction Function";
int localInt = 0xdeadbeef;
char localString2[10];
strcpy(localString2,param);
return 1;
}
int main(int argc, char *argv[])
{
char *localString = "Main Function";
int localInt = 0x11223344;
functionFunction(argv[1]);
return 0;
}
Try with Installing gcc-multilib:
this might (not 100% sure) work:, but in my scenario it did work;
sudo apt-get install gcc-multilib
Code:
#include <stdio.h>
#include <sched.h>
#include <stdlib.h>
#include <sys/wait.h>
#define _GNU_SOURCE
void *stack_memory()
{
const int stackSize = 65536;
void* stack = (void*)malloc(stackSize);
if (stack == NULL) {
printf("%s\n", "Cannot allocate memory \n");
exit(EXIT_FAILURE);
}
return stack;
}
int jail(void *args)
{
printf("Hello !! - child \n");
return EXIT_SUCCESS;
}
int main()
{
printf("%s\n", "Hello, world! - parent");
clone(jail, stack_memory(), SIGCHLD, 0);
return EXIT_SUCCESS;
}
Error:
Undefined symbols for architecture x86_64: "_clone", referenced
from:
_main in docker-4f3ae8.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code
1 (use -v to see invocation)
Linux doesn't prefix symbols with a leading _ so you're not using Linux.
But the clone(2) system call is Linux-specific, according to its man page.
clone() is Linux-specific and should not be used in programs intended
to be portable.
Probably you're using OS X or something. And you're compiling as C, so calling an un-declared function isn't a compile-time error (just a big warning). This is why it's a linker error instead of a compile-time error (and you ignored compiler warnings.)
And BTW, #define _GNU_SOURCE after including header files is pointless. You have to define feature-request macros before including headers to get them to define prototypes for GNU-only functions in cases where that's not already the default.
I am new to c. I am playing with the Mac OSX copyfile.h function to move a file using c. Header file found here: http://bit.ly/IGMSec
int copyfile(const char *from, const char *to, copyfile_state_t state, copyfile_flags_t flags);
Here is my code:
#include <copyfile.h>
int main() {
int success;
const char* from = "hello.text";
const char* to = "/toGo/hello.txt";
copyfile_state_t state = copyfile_init();
copyfile_flags_t flags = "COPYFILE_MOVE";
success = copyfile(from, to, state, flags);
printf ("%d\n", success);
exit(0);
}
I added copyfile_init() function to initialize the state. And I get compiling issues now but I think I am getting in the right direction.
$ gcc move.c
Undefined symbols for architecture x86_64:
"_copyfile_init", referenced from:
_main in ccXJr5oN.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Based on what I've seen online this is a linking issue. So I added the link tag but it's not finding the file, and this header is supposed to be in OSX source.
$ gcc move.c -lcopyfile
ld: library not found for -lcopyfile
COPYFILE_MOVE is a flag constant. It doesn't belong in a string. To actually transfer the file's data, you'll also need to set the COPYFILE_DATA flag.
Also, you can leave out the state parameter. (You didn't bother to initialize it anyway.) Just pass NULL instead.
This should work:
#include <stdio.h>
#include <copyfile.h>
int main() {
int success;
const char* from = "hello.text";
const char* to = "/toGo/hello.txt";
copyfile_flags_t flags = COPYFILE_MOVE | COPYFILE_DATA;
success = copyfile(from, to, NULL, flags);
printf ("%d\n", success);
return 0;
}
I'm getting some problems on compiling a very very simple name.c file on Mac OSX Lion.
Now, I started following Harvard CS50 course on cs50.net. I'm not totally new to programming but I was curious on how this course has been taught.
This is the source of name.c:
#include <stdio.h>
#include <cs50.h>
int
main(void)
{
printf("State your name:\n");
string name = GetString();
printf("O hai, %s!\n", name);
return 0;
}
As you can see, it requires this library: https://manual.cs50.net/CS50_Library.
Now, when I compile it, this happens:
Undefined symbols for architecture x86_64:
"_GetString", referenced from:
_main in name-vAxcar.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: *** [name] Error 1
If I use the same GetString() cs50.c function inside my source file, it works perfectly:
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
typedef char *string;
string GetString(void);
int
main(void)
{
printf("State your name:\n");
string name = GetString();
printf("O hai, %s!\n", name);
}
string
GetString(void)
{
// CODE
}
Why does this happen?
I installed the library as it says on the link above; I checked and both cs50.h and libcs50.a are respectively in /usr/local/include and /usr/local/lib.
Thank you in advance for your help.
The problem you encounter is in the linking stage, not compiling. You did not provide the implementation of GetString, only its declaration (through the .h file you #include).
To provide the implementation itself, you usually need to link against the library which includes it; this is usually done by the -l flag to g++. For example,
g++ file.cpp -lcs50
Your second sample code does link, because you manually (and explicitly) provide an implementation for GetString, though an empty one.