I recently started learning C and must create program that scanf two integer values from standard input separated by a space then printf the sum of these two integers. Must be able to accept negative values. I'm using repl.it to write code 1st then pasting in .c to compile.
Attempt:
#include <stdio.h>
int main() {
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d", &number1);
scanf("%d", &number2);
// calculating sum
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}
[OP originally said "Except this prints" -- but this is not program output -- this is error output during the compilation process before the program ever ran]
Except when I try to compile the IDE outputs the errors
/tmp/t2-8eec00.o: In function `main':
t2.c:(.text+0x0): multiple definition of `main'
/tmp/t1-f81f83.o:t1.c:(.text+0x0): first defined here
/tmp/t3-72a7ab.o: In function `main':
t3.c:(.text+0x0): multiple definition of `main'
/tmp/t1-f81f83.o:t1.c:(.text+0x0): first defined here
/tmp/main-2c962b.o: In function `main':
main.c:(.text+0x0): multiple definition of `main'
/tmp/t1-f81f83.o:t1.c:(.text+0x0): first defined here
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
exit status 1
The output is wrong so what mistake did I make? What's the correct method to get expected values?
(eg 1+2=3)
platform using it on:
https://imgur.com/a/9E8RzAO
This is a project management issue. The IDE shows that you have 4 files, all of which conflict with each other. You have t1.c, t2.c, t3.c, and main.c. They all try to define main(), so in fact you have a 4-way conflict.
Functions in C exist in a global namespace across the whole project.
Remove all files from the project that don't have the version of main() you actually want, and re-compile. -- OR rename the functions in the other files to mutually distinct names different than main(). You may get a warning that these functions are never used, but the project will compile.
The error is saying that you have defined the main function multiple times, try to compile it in a new file. The code is correct, so try to compile the code by creating new file.
Related
I've been experimenting with openmp and some math functions in C. If I try to declare and initialize some variables outside a parallel construct, then use them within a math function inside the parallel, when I compile using gcc -fopenmp practice.c -o practice I get the following error:
/usr/bin/ld: /tmp/ccQj4iIQ.o: in function `main._omp_fn.0':
practice.c:(.text+0xb3): undefined reference to `fmax'
collect2: error: ld returned 1 exit status
This issue happens with fmax, fmin, sqrt, pow, cos, etc. Some sample code that illustrates this is:
#include <omp.h>
#include <math.h>
void main(void){
double m=1;
double a=12;
#pragma omp parallel
{
m = fmax(m,a);
}
}
I've found that the issue goes away if I 1) move the fmax outside the parallel, or 2) re-initialize the variables inside the parallel, or 3) use fmax on 1 and 12 directly instead of m and a. This issue also does NOT occur if I simply try to use printf to print m and a inside the parallel, so I know each thread can "see" the values correctly.
Why is this happening, and is there a way to fix it other than the 3 things I've already tried? So far it seems like 2) is my best bet, but it seems silly to have to do initialization immediately inside the parallel when it would make more sense to do it beforehand.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I am building a program that calculates the surface area of a sphere. I also made an additional feature that allows the user to input a positive number only. Before setting my loop, I defined the function for the surface area, but it wouldn't compile because the function is not defined. The code is displayed:
include <stdio.h>
#define PI 3.1415
float sa_sphere(float r);
int main()
{
float r;
do{
printf("Enter a positive radius: ");
scanf("%f", &r);
if (r<0){
printf("Error: number entered is not positive.\n");}
}
while (r<0);{
printf("The surface area is: %.2f\n",sa_sphere(r));}
return 0;
}
I am using Linux Mint to compile it — gcc gg.c then ./a.outwhere gg is my file name.
/tmp/ccRUfp76.o: In function `main':
gg.c:(.text+0x6e): undefined reference to `sa_sphere'
collect2: error: ld returned 1 exit status
I would appreciate any tip to solve it. Please don't display the code in the answer, though.
You did not define function float sa_sphere(float r); so during linking compiler raised linking error. You must define the function in your code. If your are new please check what is function definition means. GOOD LUCK.
This question already has answers here:
What is the difference between a definition and a declaration?
(27 answers)
Closed 5 years ago.
I have two .c files, one of them has the definition of x, and the other file is using x, as follows:
file1.c:
int x;
//other code...
main.c:
int main(void)
{
printf("%d", x);
}
Now, when I compile this code, I get the following compilation error message:
In function 'main':
error: 'x' undeclared (first use in this function)
So, if a global variable (in this case x) is 'extern' by default, then why can't main.c file see x?
When I now go to main.c and define x, so that main.c now looks like:
int x=9; //This line was added.
int main(void)
{
printf("%d",x);
}
And also initialize x in file1.c, the program doesn't compile and I get the following error message:
error: ld returned 1 exit status
So, if main.c can't see x that is in file1.c, then this time what is the problem?
Is this a linking error?
Note that when I add
extern int x;
in main.c, the problem disappears.
Each compilation unit (in this case your individual .c files) is compiled separately. The compiler needs to know the storage class of x in order to handle it, so your first error (undeclared) comes from the compiler not knowing what x is. The compiler does not need to know where x lives.
When you then link your compiled objects together, the linker resolves any external names (including x in main.c if you've marked it extern) and the final executable will then have all its variables in known places. If it finds 2 extern symbols with the same name, then it will fail, giving you your second error (error: ld returned 1 exit status).
You must declare you variable in main.c, so the compiler knows about it: extern int x. The compiler said it to you: error: 'x' undeclared
You added the second definition of x in main.c, the first definition you did in file1.c. The linker informed you about ambiguity between two definitions. You could read the error above the line error: ld returned 1 exit status
I'm an absolute beginner to C and I've read a few books but never really played with it. I'm starting to try to apply what I've read with a very simple program that returns the sin of a number. The hardest thing I've encountered with C is knowing how and when to use pointers.
I'm sure this is simple but here is how I've written my test:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x;
printf("Enter a number to calculate the sin(x): \n");
scanf("%lf", &x);
printf("sin(%lf) = %lf\n", x, sin(x));
return 0;
}
I'm compiling and executing this code in Ubuntu
gcc -lm sinCalc.c && ./a.out
Error I'm receiving is this:
/tmp/blaha.o: In function `main':
sinCalc.c:(.text+0x31): undefined reference to `sin'
collect2: ld returned 1 exit status
Undefined symbols are resolved left to right, so
gcc sinCalc.c -lm && ./a.out
should work.
Are they [structs] like an interface in Java?
No. Structs are an aggregate of a number (1 or more) of types that can be dealt with as a single unit in certain circumstances (assignment, parameter passing).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
undefined reference to pthread_create in linux (c programming)
I am trying to implement Thread chain in Ubuntu in C. When I compile the following code, I get the errors of Undefined reference to these thread library function even though I have added the header file.I am also getting segmentation fault error. Why is that? I am not accessing some uninitialized memory anywhere in program. Here is the code:
#include <stdio.h>
#include<stdlib.h>
#include <pthread.h>
void* CreateChain(int*);
int main()
{
int num;
pthread_t tid;
scanf("Enter the number of threads to create\n %d",&num);
pthread_create(&tid,NULL,CreateChain,&num);
pthread_join(tid,NULL);
printf("Thread No. %d is terminated\n",num);
return 0;
}
void* CreateChain(int* num )
{
pthread_t tid;
if(num>0)
{
pthread(&tid,NULL,CreateChain,num);
pthread_join(tid,NULL);
printf("Thread No. %d is terminated\n",*num);
}
else
return NULL;
return NULL;
}
I am getting following warnings and the Scanf prompt is not appearing for some reason.
Regards
The pthread.h header file provides a forward declaration of pthread functions. This tells the compiler than these functions exist and have a certain signature. It doesn't however tell the linker anything about where to find these functions at runtime.
To allow the linker to resolve these calls (decide where to jump to inside your code or in a different shared object), you need to link against the appropriate (pthread) library by adding
-pthread
to your build command line.
[Note that it is also possible to use -lpthread. This previous question expains why -pthread is preferable.]
There are various other issues with the code that will be worthy of attention
The scanf line should be split into printf("Enter number of threads\n");scanf("%d", &num); to get the user prompt displayed
The signature of CreateChain is wrong - it should take a void* argument instead. You can always do something like int num = *(int*)arg; inside the function to retrieve the number of threads.
The logic inside CreateChain looks wrong. You currently compare a pointer against 0 - I presume you mean to compare the number of threads instead? Also, if you don't decrement the number of threads to create somewhere, you'll end up with code that creates threads forever (or until you run out of handles depending on how the different threads get scheduled).
Try compiling like this below :
gcc -Wall -pthread test.c -o test.out
-pthread is an option to tell linker explicitly to resolve the symbols related to <pthread.h>
add -lpthread
gcc -o test test.c -lpthread