Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Xcode Mac OS X 10.7.5 doesn't compile file with math header

Hello!

I've written a simple program (just at the beginning of my programming path). A few people have tested it on their machines, and it works: their Xcode compiles the program. My Xcode does not.

Here is the program.

#include <math.h>
#include <stdio.h>

int main (void)
{
float x = 2.55, y1, y2 ;
int i = 3, z = 2 ;
y1 = pow( x, i ) ;
y2 = pow( x, z ) ;
float result ;

result = 3 * y1 - 5 * y2 + 6 ;
printf ("If x equals %f, then the given polynomial equals %f\n", x, result) ;

return 0 ;
}

Here is what I get after trying to compile:

alla [11:41 AM]
95-28-37-123:~ Alla$ gcc Prog3task6add.c -o Prog3task6add
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Besides this program, programs with simple #include header work perfectly.

I use Mac OS X 10.7.5 Scode 4.6.3

Thank you very much!





MacBook Pro, Mac OS X (10.7.5), Xcode 4.6.3

Posted on Apr 23, 2015 9:40 AM

Reply
13 replies

Apr 23, 2015 1:34 PM in response to Ducol

Try reinstalling Xcode and any command line tools from the same era. Your OS and Xcode are several years old. It is going to be tough finding anyone who remembers or who has a similar setup where they can test. It might be a 32-bit vs. 64-bit issue. Including math.h may require 64-bit libraries that you don't have for some reason.

Apr 24, 2015 6:41 AM in response to Ducol

You can use the "file" command to see the architectures inside a library.


Also look for command-line flags for gcc. It may be trying to compile "native" code for your architecture, which is (maybe) 32-bit. (long story there). But you could give it a flag to force it to compile 64-bit code. That should link properly and will likely run fine too.


Here is the flag: -arch x86_64


Again, I just have to hope that will work because I think you are right in that transition era between the old GCC compiler and the new compilers. I literally have no idea what your Xcode toolchain looks like. I remember that it wasn't pretty back then. 🙂

Apr 24, 2015 11:02 AM in response to etresoft

Thank you very much for your suggestions. I will do my best to decipher them 🙂 See, I am at the very beginning of all this. I know that my system is 64-bit (Intel Core i7). Seems that xcode tries to compile 32-bit for some reason; these matters are beyond my knowledge (for now, just for now, I am growing, rapidly 🙂 )

The best way would be to upgrade my OS, but I am afraid of doing so, because I am not a pro yet, and don't know how to control the process, so to make sure everything works afterwards (especially, Microsoft office).

Apr 25, 2015 9:48 AM in response to Ducol

Try this:

gcc -arch x86_64 Prog3task6add.c -o Prog3task6add


It is possible you might need other flags too. For whatever reason, either Lion was flaky or your system is scrambled, your libraries are out of sync. Essentially you have to do cross-compilation. If the above command doesn't fix it, there are some additional flags that might help. Let's try the easiest solution first.

Apr 25, 2015 2:35 PM in response to Ducol

Your code compiles and runs cleanly on my 2009 and 2011 Mac mini using the following:

  • OS X 10.7.5 (2009 mini)

    Xcode 4.6 command-line tools

    • /usr/bin/gcc -> /usr/bin/llvm-gcc-4.2
    • /usr/bin/clang
  • OS X 10.9.5 (2011 mini)
    • Xcode 6.2 command-line tools
      • /usr/bin/gcc
      • /usr/bin/clang
    • GNU C (4.9.2 via homebrew)


All compile sequences were:

  • gcc -O2 -o Prog3task6add Prog3task6add.c
  • clang -O2 -o Prog3task6add Prog3task6add.c


All results above produced identical output:

If x equals 2.550000, then the given polynomial equals 23.231621

Apr 27, 2015 12:14 AM in response to etresoft

Thank you very much. I am sorry for not getting back to you on Saturday. I have not tried that yet; bracing myself for it, because I lack confidence in performing operations due to lack of knowledge; that's why I am learning.

Interesting and confusing fact: yesterday when I was doing one of the exercises from Programming in C, I decided to use power function once again. This time it worked. So, I went back to my program (the one above), and ... switched headers, i.e. #include <stdio.h> and only then #include <math.h>; and it worked, the program was compiled, and produced a result.


I see the next post by VikingOSX confirming that my initial program works, as some other people said.


I wonder can it all happen because of the git version I have installed? Are these things related? git version 1.7.12.4 (Apple Git-37)

According to this article https://confluence.atlassian.com/display/STASH/Installing+and+upgrading+Git I have installed a very old one. Can this cause the problem with Xcode and Command-line tools?


I do appreciate your patience and your help!

Apr 27, 2015 12:21 AM in response to VikingOSX

Thank you for your reply and for testing the program. I work on MacBookPro 2011 (produced in December 2011). I see that you have 10.9.5 version installed. I am pondering over updating; but not sure how it will effect the overall performance, as well as some programs (like Microsoft Office); also I am not sure if I will have to reinstall entire set of programs and contents (directories, files, etc) after upgrading. I also heard that people are not happy with Mavericks.

Apr 27, 2015 5:06 AM in response to Ducol

I use Mavericks everyday and suffer no performance, or stability issues whatsoever on a 2011 Mac mini that has an SSD and 8GB of memory. The standard 500GB drive and 4GB of RAM delivered horrible performance with the original Lion that came on it. Your present choices for OS X upgrade are purchased Mountain Lion via the Apple online store, or Yosemite. Apple has removed Mavericks from purchase or download.


I would not put Yosemite on a 2011 Mac without first boosting memory to at least 8GB, and change the storage to an SSD. Always perform a full backup of your Mac before hardware or operating system changes.

Apr 29, 2015 2:21 AM in response to etresoft

Hello!


I have tried to use the flag in the program below. Although the program has no reference to math.h library, it returns the same error message.

/* Program to calculate the average of a set of grades and count the nuber of failing test grades */


#include <stdio.h>


int main (void)

{

int number_of_grades, i, grade;

int grade_total = 0, failure_count = 0 ;

float average ;

printf ("How many grades will you be entering? ") ;

scanf ("%i", &number_of_grades) ;

for ( i = 1; i <= number_of_grades; ++i )

{

printf ("Enter grade #%i: ", i) ;

scant ("%i", &grade) ;

grade_total += grade ;

if ( grade < 65 ) ;

++failure_count ;

}

average = (float) grade_total / number_of_grades ;

printf ("\nGrade average = %.2f\n", average) ; // Precision to two decimal points

printf ("Number of failures = %i\n", failure_count) ;

return 0 ;

}


Error message:


Undefined symbols for architecture x86_64:

"_scant", referenced from:

_main in cchfawYD.o

ld: symbol(s) not found for architecture x86_64

collect2: ld returned 1 exit status


I would be grateful for ideas on how to solve this. Can it be due to the older version of git?


Thank you so much!

Xcode Mac OS X 10.7.5 doesn't compile file with math header

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.