I just got my Netbeans tools configured,and (finally) it worked :) (I checked with Hello World). We were shifting so I have had like a 3 month gap so I though I'd make a calculator rather than some other program like a palindrome checker and bleh bleh bleh.
So here's the code :
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void addition(int,int);
void subtraction(int,int);
void mutiplication(int,int);
void division(int,int);
int main() {
int x,y,choice,redo = 1;
while(redo)
{
printf("\nWelcome to the CalC :D\nPlease make a choice\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n>");
scanf("%d",&choice);
switch(choice);
{
case '1' :
{
printf("Enter the first number\n>");
scanf("%d",&x);
printf("\nThe second number?\n>");
scanf("%d",&y);
addition(x,y);
}
case '2' :
{
printf("Enter the first number\n>");
scanf("%d",&x);
printf("\nThe number to be subtracted from %d is?\n>",x);
scanf("%d",&y);
subtraction(x,y);
}
case '3' :
{
printf("Enter the first number\n>");
scanf("%d",&x);
printf("\nThe number to be multiplied with %d is?\n>",x);
scanf("%d",&y);
multiplication(x,y);
}
case '4' :
{
printf("Enter the first number\n>");
scanf("%d",&x);
printf("\nThe number to be divided by %d is?\n>",x);
scanf("%d",&y);
division(x,y);
}
}
printf("\nWould you like to make another calculation?\n1.Yes '_' \n2.No! :p\n>");
scanf("%d",&redo);
}
return (EXIT_SUCCESS);
}
void addition(int x,int y)
{
int sum;
sum = x + y;
printf("\nThe sum of %d and %d is %d\n(Press enter to display the menu)",x,y,sum);
getch();
}
void subtraction(int x,int y)
{
int difference;
ce;
difference = x - y;
printf("The difference between %d and %d is %d\n(Press enter to display the menu)",x,y,difference);
getch();
}
void multiplication(int x,int y)
{
int product;
product = x * y;
printf("The product of %d and %d is %d\n(Press enter to display the menu)",x,y,product);
getch();
}
void division(int x,int y)
{
float quotent;
quotent = (float)x/(float)y;
printf("The quotient of %d and %d is %.2f\n(Press enter to display the menu)",x,y,quotent);
getch();
}
And this is the error I get :
"/C/MinGW/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/CaptFuzzyboots/Documents/NetBeansProjects/CalC'
"c:/MinGW/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/calc.exe
make[2]: Entering directory 'C:/Users/CaptFuzzyboots/Documents/NetBeansProjects/CalC'
mkdir -p build/Debug/MinGW-Windows
rm -f build/Debug/MinGW-Windows/main.o.d
gcc -c -g -MMD -MP -MF build/Debug/MinGW-Windows/main.o.d -o build/Debug/MinGW-Windows/main.o main.c
main.c: In function 'main':
main.c:26:5: error: case label not within a switch statement
main.c:34:9: error: case label not within a switch statement
main.c:42:9: error: case label not within a switch statement
main.c:52:9: error: case label not within a switch statement
main.c: In function 'subtraction':
main.c:78:5: error: 'ce' undeclared (first use in this function)
main.c:78:5: note: each undeclared identifier is reported only once for each function it appears in
main.c: At top level:
main.c:83:6: warning: conflicting types for 'multiplication' [enabled by default]
main.c:48:13: note: previous implicit declaration of 'multiplication' was here
nbproject/Makefile-Debug.mk:66: recipe for target 'build/Debug/MinGW-Windows/main.o' failed
make[2]: *** [build/Debug/MinGW-Windows/main.o] Error 1
make[2]: Leaving directory 'C:/Users/CaptFuzzyboots/Documents/NetBeansProjects/CalC'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory 'C:/Users/CaptFuzzyboots/Documents/NetBeansProjects/CalC'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 661ms)
I'm used to code::blocks so I don't really know what's the problem here :\
please help me out,I clearly defined the switch labels as '1','2','3','4'!
Thanks guys :)
You have an extra semicolon after switch(choice). Your code has
switch(choice);
and it should be
switch(choice)
A few other problems:
1) You misspelled multiplication when you defined the function at the top.
2) There's a stray ce; in your subtraction function.
3) You don't have any break statements in your case statements (it looks like you don't want the cases to fall through, so you probably need them)
4) choice is an int, but you're casing on chars. Since you're reading ints with scanf, you probably want to have 1,2,3,4 instead of '1','2','3','4'.
Your main issue is the extra semi-colon after switch (choice). Remove that semi-colon, and be sure to have a break after each case as well.
You also have choice declared as an integer, but your case labels are switching on a char; it would be advisable to get rid of the single quotes around each case.
do case '1' instead of case 1
write multiplication and not mutiplication
remove the "ce" from the subtraction function
remove semicolon after switch(choice)
have a 'break' in each case
at the begining, correct spelling of multiplication
remove "ce;" inside the function subtraction.
Change cases '1', '2', etc to 1, 2...
Please note that, running a normal C code from Netbeans IDE should produce the same result if you run it in Eclipse or some other IDE. Your error is noway related to Netbeans.
Related
Currently having issues compiling programs that contain multiple files, and working on files outside of main. I suspect it has something to do with the compiler, something to do with the -c command or perhaps something I messed up during installation. Any help wrapping my head around this would be a huge help, I have been asking fellow classmates and checking online but it seems the solution must be a very simple one.
[Running] cd "/Users/shawn/Desktop/c-course/M03/calc/src/" && gcc tempCodeRunnerFile.c -o tempCodeRunnerFile && "/Users/shawn/Desktop/c-course/M03/calc/src/"tempCodeRunnerFile
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)
[Done] exited with code=1 in 0.254 seconds`your text
This is the source.c file, the one I was meant to alter.
#include "source.h"
#include <math.h>
#include <stdio.h>
void simple_math(void){
float num1;
float num2;
float result;
char operator;
char space = ' ';
scanf("%f", &num1);
scanf("%c", &space);
scanf("%f", &num2);
scanf("%c", &space);
scanf("%c", &operator); //taking user input
switch(operator) {
case '+':
result = num1 + num2;
printf("%.1f", result);
break;
case '-':
result = num1 - num2;
printf("%.1f", result);
case '*':
result = num1 * num2;
printf("%.1f", result);
case '/':
result = num1 / num2;
printf("%.1f", result);
default:
printf("ERR");
}
printf("%.1f", result);
}
The source.h file just contains
void simple_math(void);
The main file contains the following, with the #include tag including the other files.
#include "source.h"
#include <stdio.h>
int main()
{
printf("\n--- Testing calculator ---\n");
simple_math();
return 0;
}
All the files need to be linked and compiled:
gcc file1.c file2.c -o myprogram
And then run the executable:
./myprogram
You may refer to this answer:
How do I link object files in C? Fails with "Undefined symbols for architecture x86_64"
Edit #1: Please find the solution as an answer to this initial post as a code example is used. Please try also to find a possible solution in What is an undefined reference/unresolved external symbol error and how do I fix it? It didn't help me, but maybe it's suitable for you.
I try to build a project which consists of each two header and application files. If I compile each file, no errors occure. If I build the project, I run into the below error.
cd 'D:\Master\M_32561\9000_A\B13-03'
P:\PortableApps\MinGW\msys\1.0\bin\make.exe -f Makefile CONF=Debug
"/P/PortableApps/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/d/Master/M_32561/9000_A/B13-03'
"/P/PortableApps/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/b13-03.exe
make.exe[2]: Entering directory `/d/Master/M_32561/9000_A/B13-03'
mkdir -p dist/Debug/MinGW-Windows
gcc -o dist/Debug/MinGW-Windows/b13-03 build/Debug/MinGW-Windows/B13-03_F1.o build/Debug/MinGW-Windows/B13-03_MAIN.o
build/Debug/MinGW-Windows/B13-03_F1.o: In function `anzeigen_artikelbestand':
D:\Master\M_32561\9000_A\B13-03/B13-03_F1.c:283: undefined reference to `ausgeben_artikelbestand_mit_listenkopf'
build/Debug/MinGW-Windows/B13-03_MAIN.o: In function `main':
D:\Master\M_32561\9000_A\B13-03/B13-03_MAIN.c:39: undefined reference to `erfassen_artikel'
D:\Master\M_32561\9000_A\B13-03/B13-03_MAIN.c:42: undefined reference to `anzeigen_artikel'
D:\Master\M_32561\9000_A\B13-03/B13-03_MAIN.c:45: undefined reference to `aendern_artikel'
D:\Master\M_32561\9000_A\B13-03/B13-03_MAIN.c:48: undefined reference to `loeschen_artikel'
collect2.exe: error: ld returned 1 exit status
make.exe[2]: *** [dist/Debug/MinGW-Windows/b13-03.exe] Error 1
make.exe[2]: Leaving directory `/d/Master/M_32561/9000_A/B13-03'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/d/Master/M_32561/9000_A/B13-03'
make.exe": *** [.build-impl] Error 2
The project consists of the following files:
B13-03_MAIN.h
B13-03_MAIN.c
B13-03_H1.h
B13-03_F1.c
The undefined functions are declared in B13-03_MAIN.h and defined in B13-03_MAIN.c. Both application files are using B13-03_MAIN.h as a prototype.
Thanks for any help is much aprreciated.
The issue dealt with an incorrect nesting of the undefined functions. I focus on B13-03_MAIN.h and B13-03_MAIN.c.
/* B13-03_MAIN.h */
create_article();
show_article();
edit_article();
delete_article();
Implementation w/ issue:
/* B13-03_MAIN.c */
#include <stdio.h>
#include "B13-03_MAIN.h"
#include "B13-03_H1.h"
void main(void) {
switch(option) {
case abc_c: create_article(); break;
case abc_s: show_article(); break;
case abc_e: edit_article(); break;
case abc_d: delete_article(); break;
default: printf("Undefined option.");
}
void create_article(void) {
...
}
void show_article(void) {
...
}
void edit_article(void) {
...
}
void delete_article(void) {
...
}
} /* This curly braces is incorrectly nested */
Implementation w/o issues:
void main(void) {
switch(option) {
case abc_c: create_article(); break;
case abc_s: show_article(); break;
case abc_e: edit_article(); break;
case abc_d: delete_article(); break;
default: printf("Undefined option.");
}
} /*Incorrectly nested curly brace should be implemented here */
void create_article(void) {
...
}
void show_article(void) {
...
}
void edit_article(void) {
...
}
void delete_article(void) {
...
}
Have 3 files-
1.atm.c(Source file)
2.transactions.h(function declarations)
3.transactions.c(defining the functions)
when i compile(GCC) this i am getting a WinMain Error.
And i tried all the ways i know, that i can compile the program.
Ex 1:
gcc -o atm.c transactions.c transactions.h //the atm.c is getting deleted in this way.
Ex 2:as i already included the file(.h) in source so i didn't give the .h in compile time :
gcc -o atm.c transactions.c //in this case file is not getting deleted but getting the WinMain Error.
** OUTPUT:**
gcc -o atm.c transactions.c transactions.h
C:/crossdev/src/mingw-w64-v4-git/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
atm.c:
#include "transactions.h"
int main(void) {
initializeAccount();
getBalance();
//Perform first transaction
askCustomer();
updateAccount(amount);
getBalance();
//Perform second transaction
askCustomer();
updateAccount(amount);
addGift(5.0);
getBalance();
//Perform third transaction
askCustomer();
updateAccount(amount);
addGift(2.0);
getBalance();
thankYou();
return EXIT_SUCCESS;
}
transactions.h:
#include <stdio.h>
#include <stdlib.h>
#ifndef TRANSACTIONS_H_
#define TRANSACTIONS_H_
float accountBalance, amount;
void initializeAccount();
void getBalance(void);
void askCustomer(void);
void updateAccount(float value);
void addGift(float giftAmount);
void thankYou(void);
#endif
transactions.c :
#include <stdio.h>
#include <stdlib.h>
float accountBalance, amount;
void initializeAccount(void){
accountBalance = 0.0;
}
void addGift(float giftAmount){
accountBalance += giftAmount;
printf("A gift of $%.2f has been added to your \n",giftAmount);
}
void askCustomer(void){
printf("Next transaction please...\n");
printf("Enter amount to credit (positive) or debit (negative):");
scanf("%f",&amount);
}
void getBalance(void){
printf("\ncurrent balance is $%.2f\n", accountBalance);
}
void updateAccount(float amount){
accountBalance += amount;
printf("The account was updated with $%.2f\n",amount);
}
void thankYou(void){
printf("------ Thank you! ------");
}
-o is used to name the binary executable which is the output of the program. It should be followed by a file name.
You tell gcc that the linked executable should be named atm.c. Which is incorrect, but also causes that file to not get compiled or linked.
One way to correctly compile:
gcc -std=c99 atm.c transactions.c -o atm.exe
I am attempting to create a makefile for a simple program which relies on a simple file and a function stored in a c file. Here are the files:
function.c:
int random_fun(int n, int m)
{ int g;
n = n+m;
m=n/3;
g=n*m;
return g;
}
main.c:
#include <stdio.h>
#include "function.c"
int main()
{
int a, b;
printf("Enter numbers a, and b: ");
scanf("%d %d", &a, &b);
printf("Here is ur answer: %d", random_fun(a, b));
return 0;
}
And here is my makefile:
OBJS = main.o function.o
program: $(OBJS)
$(CC) -o $# $?
clean:
rm $(OBJS) program
Whenever I try type make, I get the following error:
duplicate symbol _random_fun in:
main.o
function.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
make: *** [program] Error 1"
Not sure what I am doing wrong. I can compile each code separately and main works. I was getting the same error for another project I was working on, so I tried with a very simple case involving only these 2 C files, and I get the same issues. I am fairly new to makefiles and what not, so bear with me if I am making a stupid mistake.
You should read about difference between definition and declaration in C.
As you're including function.c into your main.c, your function random_func is defined two times. Linker can't decide for you which one to use, so it errors out.
For your use case you should declare random_func in main.c or additional header file.
This is what happens with your files after preprocessing:
// function.c
int random_fun(int n, int m)
{ int g;
n = n+m;
m=n/3;
g=n*m;
return g;
}
-
// main.c
// contents of stdio.h goes first. I omit it for brevity
int random_fun(int n, int m)
{ int g;
n = n+m;
m=n/3;
g=n*m;
return g;
}
int main()
{
int a, b;
printf("Enter numbers a, and b: ");
scanf("%d %d", &a, &b);
printf("Here is ur answer: %d", random_fun(a, b));
return 0;
}
It means that now you have the same function in two separate files. When you compile both of them the linker sees two valid functions random_fun, it simply does not know which one to use.
There two ways to solve this problem.
Using header
In this case, you would need to create another file, e.g. function.h:
// random.h
int random_fun(int n, int m);
Then, in main.cyou include the header instead of .c:
#include <stdio.h>
#include "function.h" // <-- .h, not .c
int main()
{
int a, b;
printf("Enter numbers a, and b: ");
scanf("%d %d", &a, &b);
printf("Here is ur answer: %d", random_fun(a, b));
return 0;
}
This way you will have only one random_fun function across two files, the linker would not be confused.
Using extern keyword
In your main.c you can define the random_fun function as external. It basically says to a compiler that the function exists somewhere and it will be resolved later by a linker.
#include <stdio.h>
extern int random_fun(int n, int m);
int main()
{
int a, b;
printf("Enter numbers a, and b: ");
scanf("%d %d", &a, &b);
printf("Here is ur answer: %d", random_fun(a, b));
return 0;
}
Again, in this case, you will have just one random_fun function across two files and the linker would not be confused.
As a rule of thumb, I would say you never include .c files unless you absolutely need to. (I can hardly imagine when it may be needed.)
This question already has answers here:
Undefined reference to sqrt (or other mathematical functions)
(5 answers)
Closed 5 years ago.
So I'm trying to make a program that calculates the quadratic formula, but when I try to compile the code, I get the following:"undefined reference to sqrt"
But I tried defining sqrt via math.h and 2 other times in the code.
I have attached my code
Any help would be greatly appreciated
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double sqrt(double);
int main (void) {
double sqrt(double);
int a,b,c;
double discriminant,squarerootofdis,root1, root2;
printf("Please enter the coefficient of x^2:");
scanf("%d",&a);
printf("Please enter the coefficient of x:");
scanf("%d",&b);
printf("Please enter the integer value of the ploynomial:");
scanf("%d",&c);
if (a==0 && b==0)
{printf("This case is extremely degenerate");}
else if (a==0 && b!=0)
{root1=-c/b;
printf("Degenerate one real root: %lf\n",root1);}
else{
discriminant = ((b*b)-(4*a*c));
squarerootofdis = sqrt(discriminant);
root1 = (squarerootofdis-b)/(2*a);
root2 = (-squarerootofdis-b)/(2*a);
if (discriminant>0)
printf("Two real roots: %lf\n %lf\n", root1, root2);
else if (discriminant == 0)
printf("Degenerate one real root: %lf\n",root1);
else if (discriminant<0)
printf("Two complex roots: %lf\n %lf\n", root1, root2);
}
}
To use the sqrt function (or any function defined in math.h), you'll have to link the m library:
~$ gcc -lm yourcode.c -o program
Did you compile with -lm linked?
Header file will provide the decalration to the sqrt() function. To have the definition, you need to link with the math library consisting of the function definition.
Example:
gcc test.c -o output -lm
Please use the below command
gcc test.c -lmath