I am trying to do a simple strrev on a string and I keep getting this error when I compile it on my mac
Undefined symbols for architecture x86_64:
"_strrev", referenced from:
_main in cc1zSAum.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
My code is:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[]){
char str[] = "Hello world";
char * test;
test = strrev(str);
printf("%s",test);
return 0;
}
I tried playing around with the strrev line
but nothing is working
Any help would be appreciated
Thanks
There's no standard C function by that name. I did a little Googling and it looks like a few compilers and/or C library implementations have included such a function as a non-standard extension, but you can't expect any arbitrary library to have one.
Would be simple enough to write your own, though -- could be a one line loop body, even.
Related
i have a c file named main.c
#include <stdio.h>
extern int main(int argc, char* argv[], char* envp[]);
void start(){
...;
int return_code = main(argc, argv, envp);
exit(return_code);
}
you can see I declared main but when using ld to link it:
$ (use ld to link, I didn't write it down because it's quite verbose and irrelevant)
ld: bin/test.o: in function `start':
/home/user/Desktop/test/test.c:28: undefined reference to `main'
make: *** [Makefile:49: link] Error 1
so what should i do (sorry if this is a simple question for you)
In C you have to define a main function that will be called automatically by your program, this is the base of your code.
I saw that you include "stdio.h" which is a library allowing to have access to some function like for example in my program the function "printf".
If you don't need it then don't include it :)
For example here is how to make your first program with a main function.
#include <stdio.h>
int main(int argc, char *argv[]){
... // Your code
printf("Hello world"); // Just print on your terminal this string
return (0); // 0 is the default return code if there is no errors
}
Generally speaking, invoking ld yourself is being a glutton for punishment. Use your C compiler to link until proven otherwise.
gcc -o bin/test bin/test.o will link a C program for you.
It looks like you tried to "fix" it by providing _start yourself. You can't (in C). _start is not a function.
I've been asked to write test programs on 4 functions in the card game dominion. I've written one (extremely simple) just to make sure I can get it to pass as I'm pretty new to testing. However, I continually get a syntax error at runtime that I cannot figure out.
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
int main() {
int r = 0, j = 0;
int adventurer = 8;
int greathall = 17;
r = getCost(adventurer);
assert(r == 6);
j = getCost(greathall);
assert(j == 3);
return 0;
}
When I compile it, I do get some warnings:
Undefined symbols for architecture x86_64:
"_getCost", referenced from:
_main in unittest1-7d7bf2.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Which I'm not sure about either, but the base code that we are given from our instructor, as well as all of the other code from my classmates, has these warnings as well.
However, when running I get the following error:
./unittest1.c: line 8: syntax error near unexpected token `('
./unittest1.c: line 8: `int main() {'
I've tried rewriting it in a blank file thinking there was some invisible characters or something but I still get this error. Does anyone see something wrong in my code? Any help is appreciated.
**getCost is called in dominion_helpers
When I run this code in Xcode, I get build failed. I got the chunk from The Big Nerd Ranch Guide to Obj-C. I had to modify it a little (added the libraries stdlib.h and readline/readline.h) It says the build failed, but there are no errors that I can see. This question may look like a duplicate, and in a way it is, but even after seeing their solutions and trying them for myself, I still get the error.
#include <stdio.h>
#include <readline/readline.h>
#include <stdlib.h>
int main(int argc, const char * argv[])
{
printf("Who is cool? ");
const char *name = readline(NULL);
printf("%s is cool!\n\n", name);
return 0;
}
Undefined symbols for architecture x86_64: "_readline", 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)
A similar problem is mentioned in this link. You have to link the libreadline.dylib file to your project in the build phase.
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.