Why is my pdb file missing symbols when I link object files? - c

I'm compiling a very simple C program with two source files.
File 1:
#include <stdio.h>
#include "conv.h"
/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300 */
int main()
{
int fahr;
float celsius;
int lower, upper, step;
lower = 0; /* lower limit of temperature table */
upper = 300; /* upper limit */
step = 20; /* step size */
fahr = lower;
while (fahr <= upper) {
celsius = f2c(fahr);
printf("%d\t%0.2f\n", fahr, celsius);
fahr = fahr + step;
}
return 0;
}
File 2:
#include "conv.h"
/* Convert Fahrenheit to Celsius */
float f2c(int fahr) {
float celsius;
celsius = (fahr-32) * (5.0 / 9.0);
return celsius;
}
/* Convert Celsius to Fahrenheit */
float c2f(int cel) {
float fahr;
fahr = cel * (9.0 / 5.0) + 32;
return fahr;
}
I'm 'debugging' the program in windbg, although the program runs fine -- it's for experimentation. I'm compiling using the command-line Developer Command Prompt for VS 2019 in two ways. In the first case, I run the whole thing through CL:
cl /Zi /Fd:fahr fahr.c conv.c
and in the second case, I'm compiling without linking, then linking the object files:
cl /c fahr.c conv.c
cl /Zi /Fd:fahr fahr.obj conv.obj
In the first case, I can debug in windbg just fine. I open the executable, set a breakpoint, run it, and up comes my source file. But when I compile the second way, windbg will no longer connect to my source, and it fails to find variables in fahr.c and conv.c. As far as I can tell, everything is the same except for the circumstances when I link. What am I missing?

The debug file (.pdb) is added to in stages as each part of the program is built. When you compile files, the compiler adds their symbols to the pdb file (if you ask it to). Then, when you link files, it adds further symbols to the pdb file. I wasn't getting source file symbols because I didn't generate any debug data when I compiled the object files. The fix is:
cl /c /Zi /Fd:fahr fahr.c conv.c
cl /Zi /Fd:fahr fahr.obj conv.obj
My mistake was that I thought the debug file was generated by the linker only, which never sees the source files. Rather both the compiler and the linker have a role in producing the debug file.

Related

giving parameters into math function

If I call in C, math function `"trunc" in math library is define as:
extern double trunc _PARAMS((double));
and in my main file call it:
int i = (int)trunc(2.5);
It works fine, no problems. But if I try to pass double passively, like:
double d = 2.5;
int i = (int)trunc(d);
It won't work?!? In my microprocessor STM32F4 IDE it goes in debugger mode into:
Infinite_Loop:
b Infinite_Loop
and it stuck there. I also change double and try float, int, unit8_t,... no one isn't working.
Also other math functions will work fine as I call them like this:
i = sin(1);
i = cos(1);
But, it will crashed the same, if called like this:
int a = 1;
i = sin(a);
i = cos(a);
I am running this code on microprocessor STM32F4 Discovery,IDE is Eclipse Ac6.
If you refer the man page for trunc() you should note that, it says
Link with -lm
So, make sure that you are linking with -lm.
Secondly, when synopsis say double trunc(double x);, there is no point in trying it out with other data types.
If you want to try with other data types then you should look for these variants:
float truncf(float x);
long double truncl(long double x);
Test code:
#include<stdio.h>
#include<math.h>
int main(void)
{
// using trunc function which return
// Truncated value of the input
double d = 2.7;
printf(" Truncated value is %d \n", (int)trunc(d));
return 0;
}
This generated an output:
Truncated value is 2
And it was compiled with gcc version 5.3.0
When I compile the same code gcc version 7.2.0 without -lm option,
I get following warning:
undefined reference to `trunc'
error: ld returned 1 exit status

error: "the procedure entry point.." when running exe after compiling with gcc / cygwin

I am learning c and I am using code::block
I have wrote this code "from ansi c book"
#include <stdio.h>
#include <stdlib.h>
float convertToCelsius(float f);
int main()
{
int start = 0;
int step = 5;
int upper = 300;
printf("%3c\t%6c\n",'F','C');
while(start < upper){
printf("%3d\t%6.3f\n", start, convertToCelsius(start));
start += step;
}
return 0;
}
float convertToCelsius(float f){
return (5.0/9)*(f-32);
}
when I run the code from the IDE "code::blocks" it compile and run without problems
but when I compile the c file using gcc in cygwin and try to run the exe file it gives me this message
the procedure entry point __cxa_atexit could not be located in the
dynamic link library C:\cygwin\home\username\convert.exe
I have search but couldn't find related direct answer
what is the problem ?

C program to find roots error

I am writing a function in C with the below specifications:
float find_root(float a, float b, float c, float p, float q);
find_root takes the coefficients a,b,c of a quadratic equation and an interval (p, q). It will return the root of this equation in the given interval.
For example: find_root(1, -8, 15, 2, 4) should produce a root "close to" 3.0
I have written the below code, and I don't understand why it doesn't work:
#include<stdio.h>
#include<math.h>
main()
{
printf("Hello World");
}
float find_root(float a, float b, float c, float p, float q) {
float d,root1,root2;
d = b * b - 4 * a * c;
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
if (root1<=q || root1>=p)
{
return root1;
}
return root2;
}
Please let me know what the error is.
Your program doesn't work, because, you never called find_root() from your main().
find_root() is not suppossed to run all-by-itself. Your program statrs execution from main(). You need to call your sub-function from main() in order to make them execute.
Change your main to have a call to find_root(), something like below.
int main() //put proper signature
{
float anser = 0;
answer = find_root(1, -8, 15, 2, 4); //taken from the question
printf("The anser is %f\n", answer); //end with a \n, stdout is line buffered
return 0; //return some value, good practice
}
Then, compile the program like
gcc -o output yourfilename.c -lm
Apart from this, for the logical issue(s) in find_root() function, please follow the way suggested by Mr. #paxdiablo.
For that data, your two roots are 5 and 3. With p == 2 and q == 4:
if (root1<=q || root1>=p)
becomes:
if (5<=4 || 5>=2)
which is true, so you'll get 5.
The if condition you want is:
if ((p <= root1) && (root1 <= q))
as shown in the following program, that produces the correct 3:
#include<stdio.h>
#include<math.h>
float find_root (float a, float b, float c, float p, float q) {
float d,root1,root2;
d = b * b - 4 * a * c;
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
if ((p <= root1) && (root1 <= q))
return root1;
return root2;
}
int main (void) {
printf ("%f\n", find_root(1, -8, 15, 2, 4));
return 0;
}
That's the logic errors with your calculations of the roots.
Just keep in mind there are other issues with your code.
You need to ensure you actually call the function itself, your main as it stands does not.
It also wont produce a value within the p/q bounds, instead it will give you the first root if it's within those bounds, otherwise it'll give you the second root regardless of its value.
You may want to catch the situation where d is negative, since you don't want to take the square root of it:
a = 1000, b = 0, c = 1000: d <- -4,000,000
And, lastly, if your compiler is complaining about not being able to link sqrt (as per one of your comments), you'll probably find you can fix that by specifying the math library, something like:
gcc -o myprog myprog.c -lm
Your program starts at main by definition.
Your main function is not calling find_root but it should.
You need to compile with all warnings & debug info (gcc -Wall -Wextra -g) then use a debugger (gdb) to run your code step by step to understand the behavior of your program, so compile with
gcc -Wall -Wextra -g yoursource.c -lm -o yourbinary
or with
clang -Wall -Wextra -g yoursource.c -lm -o yourbinary
then learn how to use gdb (e.g. run gdb ./yourbinary ... and later ./yourbinary without a debugger)
Then you'll think and improve the source code and recompile it, and debug it again. And repeat that process till you are happy with your program.
BTW, you'll better end your printf format strings with \n or learn about fflush(3)
Don't forget to read the documentation of every function (like printf(3) ...) that you are calling.
You might want to give some arguments (thru your main(int argc, char**argv) ...) to your program. You could use atof(3) to convert them to a double
Read also about undefined behavior, which you should always avoid.
BTW, you can use any standard C compiler (and editor like emacs or gedit) for your homework, e.g. use gcc or clang on your Linux laptop (then use gdb ...). You don't need a specific seashell
Change this condition
if (root1<=q || root1>=p)
to
if (root1<=q && root1>=p)
otherwise if anyone of the conditions is satisfied, root1 will be returned and root2 will almost never be returned. Hope this fixes your problem.

C programming - "Undefined symbol referenced in file"

I am trying to write a program to approximate pi. It basically takes random points between 0.00 and 1.00 and compares them to the bound of a circle, and the ratio of points inside the circle to total points should approach pi (A very quick explanation, the specification goes in depth much more).
However, I am getting the following error when compiling with gcc:
Undefined first referenced
symbol in file
pow /var/tmp//cc6gSbfE.o
ld: fatal: symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
What is happening with this? I've never seen this error before, and I don't know why it's coming up. Here is my code (though I haven't fully tested it since I can't get past the error):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
float x, y;
float coordSquared;
float coordRoot;
float ratio;
int n;
int count;
int i;
printf("Enter number of points: ");
scanf("%d", &n);
srand(time(0));
for (i = 0; i < n; i++) {
x = rand();
y = rand();
coordSquared = pow(x, 2) + pow(y, 2);
coordRoot = pow(coordSquared, 0.5);
if ((x < coordRoot) && (y < coordRoot)) {
count++;
}
}
ratio = count / n;
ratio = ratio * 4;
printf("Pi is approximately %f", ratio);
return 0;
}
use -lm during compilation(or linking) to include math library.
Like this: gcc yourFile.c -o yourfile -lm
need to Link with -lm.
gcc test.c -o test -lm
The error is produced by the linker, ld. It is telling you that the symbol pow cannot be found (is undefined in all the object files handled by the linker). The solution is to include the library which includes the implementation of the pow() function, libm (m for math). [1] Add the -lm switch to your compiler command line invocation (after all the source file specifications) to do so, e.g.
gcc -o a.out source.c -lm
[1] Alternatively, you could have your own implementation of pow() in a separate translation unit or a library, but you would still have to tell the compiler/linker where to find it.

Symbolic Constant in Xcode

I'm learning C through The C Programming Language 2nd Edition and it refers to symbolic constants where you use #define before main() to assign a label to a value.
This is the program I am trying to use:
#include <stdio.h>
#define LOWER 0
#define UPPER 300
#define STEP 20
main()
{
int fahr;
for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
{
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}
}
Printing a table of Fahrenheit Celsius conversions. However This code when compiled in xcode using the c tool gives me the response unable to read unknown load command referring to the line starting with for. I've tried retyping the program but it still hasn't worked. Any help would be much appreciated.
The first line should be
#include <stdio.h>
The code is perfectly fine. There's something wrong with your XCode setup (may be related: unable to read unknown load command.
cristi:tmp diciu$ cat test.c
#include <stdio.h>
#define LOWER 0
#define UPPER 300
#define STEP 20
main()
{
int fahr;
for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}
cristi:tmp diciu$ gcc test.c
cristi:tmp diciu$ ./a.out
0 -17.8
20 -6.7
40 4.4
[..]
Works for me in XCode - the only warning/error I got was
Control reaches end of non-void function
As defining main() defaults to int it should return something, eg. 0 for a successful program. Convention suggests that 0 means a program runs correctly and anything else is an error.
Better to define
int main()
{
/* code */
return 0;
}
as your main function. But this is a digression - see diciu's answer for a potential explanation of your problem

Resources