Trouble when linking Objective-C program with my own libobjc.A.dylib - objective-c

I builded a custom libobjc.A.dylib from the source of apple's open source website and I want to debug it by compiling a simple program with the dylib, but it seems the clang always using the system libobjc.A.dylib.
The compile command is:
xcrun --sdk macosx10.8 clang main.m -std=c99 -framework Foundation -lobjc -I./usr/include -L.
the libobjc.A.dylib is in the current folder. And the otool command tells me which library the program links with:
~ otool -L a.out
a.out:
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 945.18.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 744.19.0)
In the http://opensource.apple.com/source/objc4/objc4-551.1/test/test.pl file, it's using a flag called -fno-objc-link-runtime, but it seems it's not working with my laptop(mac 10.9.4, xcode 5.1.1)
Tried setting DYLD_LIBRARY_PATH environment var, but it's not working. Here is the folder which I compiled the code https://github.com/Jeswang/Compile-Issue, any suggestion or solution?

Actually, I used install_name_tool command to change the library's link:
install_name_tool -change /usr/lib/libobjc.A.dylib #executable_path/libobjc.A.dylib main
Useful links:
dyld manual
mikeash.com: Friday Q&A 2009-11-06: Linking and Install Names

Related

Executable links correctly but fails to load a library

I have Boost and Served REST API package installed in /opt/local.
Here's the "meaningful" part of my CmakeLists.txt:
find_package(PkgConfig REQUIRED)
pkg_check_modules(SERVED_PKG REQUIRED IMPORTED_TARGET served)
add_executable( rest_server rest_server.cpp )
target_link_libraries(rest_server PUBLIC PkgConfig::SERVED_PKG)
It does create an executable, and links it with the needed library.
However, it somehow fails to include the path to that library:
$ otool -L cmake-build-debug/rest_server
cmake-build-debug/rest_server:
libserved.1.4.dylib (compatibility version 0.0.0, current version 1.4.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 902.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)
Which, when I try to run it via the CLion IDE, results in a failure to load the libserved.1.4.dylib library:
/Users/ur20980/src/security2/cmake-build-debug/rest_server
dyld: Library not loaded: libserved.1.4.dylib
Referenced from: /Users/ur20980/src/security2/cmake-build-debug/rest_server
Reason: image not found
Process finished with exit code 6
When I invoke the executable manually from the terminal, it seems to be OK:
$ cmake-build-debug/rest_server
Hello, World!
What is wrong???

Compile Objective-C with clang on macOS Sierra

I'm trying to figure out how to compile this snippet of code on macOS Sierra.
#import <Foundation/Foundation.h>
int main() {
NSLog(#"Hello World");
return 0;
}
On El Capitan, I could compile with this command.
clang -x objective-c -framework Foundation main.m
However, when I try that command on Sierra, I see these errors.
In file included from main.m:1:
In file included from /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10:
In file included from /System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5:
/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:44:12: error: unknown property attribute 'class'
#property (class, readonly) BOOL supportsSecureCoding;
This is the version of clang that I'm using.
$ clang -v
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin16.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
I have Xcode version 7.3.1 (7D1014) and Xcode version 8.0 beta 4 (8S188o) installed. The version of Sierra I have is 10.12 beta (16A254g).
Note: I want to compile this in the terminal with clang, not inside of Xcode.
Make sure you have the Xcode 8 version of the command-line tools selected.
Objective-C gained support for class properties within the new version of Clang and Xcode. Here is and article where you may find some useful information: Objective-C Class Properties.
To solve this problem you should simply install Xcode 8.
UPD
Forgot to mention:
After installation make sure that you have switched command line tools to the recent Xcode:
$ clang --version
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ xcode-select -print-path
/Applications/Xcode.app/Contents/Developer
$ sudo xcode-select -switch /Applications/Xcode-beta.app/Contents/Developer/
$ xcode-select -print-path
/Applications/Xcode-beta.app/Contents/Developer
$ clang --version
Apple LLVM version 8.0.0 (clang-800.0.33.1)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

CMake does not link glfw right under osx

20160614:
I have glfw installed by MacPorts.
sudo port install glfw
And I have checked that /opt/local/lib/libglfw.dylib is exists.
Here is my top header of main.cpp:
#include <GLFW/glfw3.h>
My CMakeLists.txt(file1):
cmake_minimum_required (VERSION 2.8)
project (t1)
include_directories(/opt/local/include)
link_directories(/opt/local/lib)
# find_library(MYGLFWLIB NAMES glfw HINTS /opt/local/lib NO_DEFAULT_PATH)
set(GLLIBS GL glfw glew)
add_executable(t1 main.cpp)
target_link_libraries(t1 ${GLLIBS})
After cmake . && make which is no error reported, I execute by ./t1
Then Error came:
dyld: Library not loaded: lib/libglfw.3.dylib
Referenced from: /Users/...../t1
Reason: image not found
[1] 13949 trace trap ./t1
Then I use otool -L t1 to check lib dependens in t1
/opt/local/lib/libGL.1.dylib (compatibility version 4.0.0, current version 4.0.0)
lib/libglfw.3.dylib (compatibility version 3.0.0, current version 3.2.0)
/opt/local/lib/libGLEW.1.13.0.dylib (compatibility version 1.13.0, current version 1.13.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
Could someone tell me why glfw is not linked rightly?
20160615:
After modifying my CMakeLists.txt(file2):
cmake_minimum_required (VERSION 2.8)
project (t1)
include_directories(/opt/local/include)
link_directories(/opt/local/lib)
find_library(GL_LIB GL HINTS /opt/local/lib )
find_library(GLFW_LIB glfw HINTS /opt/local/lib)
find_library(GLEW_LIB glew HINTS /opt/local/lib)
message(${GLFW_LIB})
message(${GLEW_LIB})
message(${GL_LIB})
add_executable(t1 main.cpp)
target_link_libraries(t1 ${GL_LIB} ${GLFW_LIB} ${GLEW_LIB})
I have two osx device(MacMini and MacBookPro), the version of MacPorts and Xcode and CMake and System are all the same:
Mac osx : 10.11.5
MacPorts 2.3.4
CMake :3.5.2
Xcode: 7.3
Both version of CMakeLists (file1 and file2 above)works in MacMini, linker works fine.
But when it cames to my MacBookPro, glfw is not linked well.
Someone else had encounter the almost same problem:
macports-cmake-make-dyld-library-not-loaded
Is this problem SYSTEM VARIABLE concerned ?
After search for hours, I found the reason : glfw #3.2: install_name is not set properly
set(GLLIBS GL glfw glew)
That's not how you should use CMake. Use find_library or
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
target_link_libraries(simple ${GLFW_LIBRARIES})
Have a look into the documentation:
http://www.glfw.org/docs/3.0/build.html

Compile Mach-o arm object file

Im trying to compile a mach-o arm object file to mach-o arm executable using the command line. I have used various commands like these
clang -arch armv7 helloapp.o -o helloapp
clang helloapp.o -o helloapp
gcc helloapp.o -o helloapp
They all return different errors saying compiling for wrong architecture or missing neccessary files. What is the command I need to compile this properly??
The default compilers (the ones in your $PATH) reference the ones that can compile for your local machine. You need a cross-compiler that knows how to create ARM binaries. If you've got Xcode with iOS SDK installed you can use for example:
PATH_TO_Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc
or
PATH_TO_Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
For example, on my machine:
ARM_GCC=~/Documents/Xcode4.6.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc
IOS_SDK=~/Documents/Xcode4.6.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk
# Compile
$ARM_GCC -arch armv7 -c test.c -o test.o
# Link
$ARM_GCC -arch armv7 -isysroot "$IOS_SDK" test.o -o test
If I then run file test I get:
test: Mach-O executable arm

Error installing mod_wsgi 3.2

I am trying to install mod_wsgi 3.2 on Mac OSX 10.6.6 and am getting this error when I attempt to make
Installed assemblers are:
/usr/bin/../libexec/gcc/darwin/x86_64/as for architecture x86_64
/usr/bin/../libexec/gcc/darwin/i386/as for architecture i386
lipo: can't open input file: /var/folders/XW/XWYalsEzG3Gkn+PhoNKF0k+++TI/-Tmp-//ccsEgbTa.out (No such file or directory)
apxs:Error: Command failed with rc=65536
.
make: * [mod_wsgi.la] Error 1
This is a late answer, but I found a solution in my searching and wanted to include it here for others. This error typically occurs because you are trying to build libraries for ppc architecture which won't work as xcode4 doesn't no longer includes support for ppc. You can get around this by setting the following in your environment before running your build (I put this in my .bash_profile so I don't beat my head against the wall later):
export ARCHFLAGS="-arch i386 -arch x86_64"
The latest mod_wsgi version is 3.3 for a start, why are you using 3.2? Second, there is a precompiled mod_wsgi.so binary for MacOS X which can be used for Apple supplied Python and Apache, so you do not need to compile it from source code and so avoid need to have installed XCode development tools. The precompile mod_wsgi.so is available from mod_wsgi download page.
As to the errors, it would appear to be a permissions problem for user that you are compiling as. That or you aren't using standard Apple supplied software and have somehow mixed up your PATH so it is grabbing disparate tools from different package distributions. Are you using just Apple tools or have you installed any of MacPorts, fink or HomeBrew?
Edit the Make file like:
CPPFLAGS = -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -DNDEBUG
CFLAGS = -Wc,"-arch i386" -Wc,"-arch x86_64" -Wc
LDFLAGS = -arch i386 -arch x86_64 -F/Library/Frameworks -framework Python -u _PyMac_Error
LDLIBS = -ldl -framework CoreFoundation
I had the same question
In the end,I found I don't have write permission of '/usr/libexec/apache2/'
then I add chmod +w to the folder,,
then install success !!
Hope my answer can be a refer for some others
Good Luck!