Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

2019-10-04

GCC 32bit compile on Mac

  • The i386 architecture is deprecated for macOS
    $ gcc -m32 fib.c
    ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)
    ld: warning: ignoring file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd, missing required architecture i386 in file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd
    Undefined symbols for architecture i386:
      "_printf", referenced from:
          _main in fib-68f6b9.o
    ld: symbol(s) not found for architecture i386
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    
  • Install i386 support
    $ sudo rm -rf /Library/Developer/CommandLineTools
    $ xcode-select --install
    $ open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
    
  • Compile
    $ gcc -m32 fib.c
    ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)
    
    $ make CFLAGS="-m32" fib
    cc -m32    fib.c   -o fib
    ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)
    

2019-06-27

Language performance test

Lower value shows better performance

  • C
    #include <stdio.h>
    
    int fib(int n) {
        if(n == 1) return 0;
        if(n == 2) return 1;
        return fib(n - 2) + fib(n - 1);
    }
    
    int main() {
        int i = 0;
        for(i = 1; i <= 40; i++) {
            printf("%d %d\n", i, fib(i));
        }
        printf("\nC\n");
        return 0;
    }
    
  • Java
    public class Fib {
     
        public static int calc(int n) {
            if(n <= 1) return 0;
            if(n == 2) return 1;
            return Fib.calc(n - 2) + Fib.calc(n - 1);
        }
        public static void main(String[] args) {
            for(int i = 1; i <= 40; i++) {
                System.out.println(i + " " + Fib.calc(i));
            }
            System.out.println("\nJAVA");
        }
     
    }
    
  • JavaScript
    #!/usr/bin/env node
    
    let range = Array.from({length: 40}, (x, i) => i+1);
    let fib = n => n === 1 ? 0 : n === 2 ? 1 : fib(n - 2) + fib(n - 1);
    
    range.forEach( i => console.log(i, fib(i)) );
    console.log("\nJavaScript");
    
  • PHP
    #!/usr/bin/env php
    <?php
    function fib(int $n) {
        if ($n === 1) return 0;
        if ($n === 2) return 1;
        return fib($n - 2) + fib($n - 1);
    }
    
    foreach (range(1, 40) as $i) {
        echo $i, ' ', fib($i), PHP_EOL;
    }
    echo "\nPHP\n";
    
  • Python
    #!/usr/bin/env python
    
    def fib(n):
      if n == 1:
        return 0
      elif n == 2:
        return 1
      return fib(n - 2) + fib(n - 1)
    
    if __name__ == "__main__":
      for i in range(1, 40 + 1):
        print(i, fib(i))
      print("\nPYTHON")
    
  • Ruby
    #!/usr/bin/env ruby
    
    def fib(n)
      return 0 if n == 1
      return 1 if n == 2
      return fib(n - 2) + fib(n - 1)
    end
    
    (1..40).each do |i|
      puts "#{i.to_s} #{fib(i).to_s}"
    end
    puts "\nRUBY"
    

2017-01-31

How to set DYLD_LIBRARY_PATH in Xcode

Open Product > Scheme > Edit Scheme > Run > Arguments
Add DYLD_LIBRARY_PATH under Environment Variables


dyld: Library not loaded: libboost_system.dylib

Got an error when compile boost library and run a sample program.
$ ./PROGRAM
dyld: Library not loaded: libboost_system.dylib
  Referenced from: /PATH/TO/PROGRAM
  Reason: image not found
Abort trap: 6
Check libraries then tried install_name_tool, but failed.
$ otool -L PROGRAM
PROGRAM:
libboost_system.dylib (compatibility version 0.0.0, current version 0.0.0)
libboost_thread.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.4.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0)

$ install_name_tool -change libboost_system.dylib /PATH/TO/libboost_system.dylib PROGRAM
$ install_name_tool -change libboost_thread.dylib /PATH/TO/libboost_thread.dylib PROGRAM
Solution, add the following to .profile or .bash_profile
export DYLD_LIBRARY_PATH=/PATH/TO/BOOST/stage/lib