Testing Dominion Card Game in C - c

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

Related

Why does this code yield an error with regards to the pow function?

#include <stdio.h>
#include <math.h>
int exp_for_level(int n) {
return (int)(100 * pow(n, 2.3));
}
int main(){
int x;
x = exp_for_level(6);
printf("%d", x);
return 0;
}
I receive the following error when I run this code on an online compiler
/tmp/cc28S7ML.o: In function exp_for_level':
main.c:(.text+0x19): undefined reference to `pow'
collect2: error: ld returned 1 exit status
How do I rectify this?
After I couldn't get it to work on the online compiler, I followed advice from some other threads on
The file is stored under a file grades.c on my mac
I've tried entering this
$ gcc - Wall - lm -o grades grade . c
into my terminal and i just get zsh error: command not found
Any ideas on what the issue is here too?
The online compiler I'm using is
https://www.tutorialspoint.com/compile_c_online.php
EDIT: in my post, in main I'd miswritten the function as exp_to_level instead of exp_for_level. Didn't copy paste the entire code as it's too long. I narrowed it down and retyped it to the portion that yields the error.
There are some errors in your code, you have defined a function exp_for_level but you use exp_to_level.
Then your x variable is not defined
If you fix your code like this:
#include <stdio.h>
#include <math.h>
int exp_for_level(int n) {
return (int)(100 * pow(n, 2.3));
}
int main(){
int x = exp_for_level(6);
printf("%d", x);
return 0;
}
and you compile:
gcc -Wall powtest.c -o powtest -lm
it works.
About the error on the online compiler:
The undefined reference error occurs because you are missing -lm linker option.
Edit the online compiler command clicking on Project->Compile Options:
About this problem on your local machine:
After I couldn't get it to work on the online compiler, I followed
advice from some other threads on The file is stored under a file
grades.c on my mac I've tried entering this
$ gcc - Wall - lm -o grades grade . c
into my terminal and i just get zsh error: command not found
you don't have the compiler installed.
You should install clang, Have a look to this question
First of all your function name is wrong in the main take a look here exp_for_level
and in main its exp_to_level change one of them then also add int x in main to solve the issue.

How to fix "undefined symbols for architecture x86_64" message?

I am writing a program using C, which, given two points, and a value x in between them, a function "Interp" will interpolate a corresponding value for y. I am receiving a message that says "undefined symbols for architecture", and I'm not sure how to fix it. Any help would be appreciated.
#include <stdio.h>
#include <math.h>
double Interp(double x_1, double y_1, double x_2, double y_2, double x) {
double k,y,b;
k = (y_2-y_1)/(x_2-x_1);
b = y_2-(k*x_2)
y = k*x+b;
return(y);
}
The result is a message saying "Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error linker command failed with exit code 1"
I don't know what this means so any guidance would be very helpful!

readline not working properly

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.

C - Undefined symbols for architecture x86_64 when compiling on Mac OSX Lion

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.

Reversing a string in C x86 error

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.

Resources