Xcode "ld: library not found" for C++ makefile project - c++

OS: Mac 10.14.6 -- Xcode ver: 11.3.1
I have an external C++ project (that uses an existing makefile to build) that I imported into Xcode, and when I try to build it I get the following error message: ld: library not found for -ljpeg.
Strangely, when I run the same build command ($make all-recursive) from the command line, it builds successfully. I've double-checked and the relevant .dylib files DO exist in /usr/local/lib. From reading around the web it seemed like Xcode build-settings needed library search paths to include the directory where the .dylib files are, so I added LIBRARY_SEARCH_PATHS under target "Build Settings", but that did not solve the problem.
Is there any reason that comes to mind why Xcode's build command would fail but mine wouldn't?

Link should also receive a flag of the form -L<path> which has the path to the jpeg library's parent folder, before the -ljpeg flag.
So add -L/usr/local/lib to the other linker flags build setting and try again.
Easy way to find the correct flags is running pkg-config:
pkg-config --libs libjpeg
-L/usr/local/Cellar/jpeg/9d/lib -ljpeg
If that fails, you can:
Copy the dylib from its original location.
Paste it in a folder #executable_path/../Resources/lib. Create those folders if needed.
Then rebuild.
Most robust way is to pass the full path in -l option, and not break it in two components.

Related

Integrating wxWidgets into Existing XCode Project

There are many tutorials about setting up a new XCode project for use with wxWidgets, but I need to integrate it with a large existing XCode C++ project.
The wxWidgets setup process is unclear to me. I built wxWidgets in XCode with the included wxcocoa.xcodeproj file. But when I include all the header files and the libwx_osx_cocoa.dylib library to my existing XCode project, I get all these errors within the wxWidgets header files like Use of undeclared identifier 'wxApp' and No matching constructor for initialization of 'wxEvent' as if it's missing files, but I've included all the header files and compiled library. I tried building wxWidgets in the terminal and get the same errors. I tried make install to actually install wxWidgets on my system. I tried running this command wx-config --cxxflags --libs all in the terminal. I tried adding wx-config --cxxflags and wx-config --libs all to the compiler and linker flags in my XCode project. None of these had any effect.
The irony is that I'm trying to use wxWidgets library to make the GUI process easier, but instead I've spent two days pulling my hair out trying to get my C++ XCode project to compile. Does anyone know what I'm doing wrong?
EDIT: The problem seems to be XCode. I've downloaded the wxWidgets Hello World sample program at the bottom of this page. I can build this program without issue in the terminal, but when I put it into a new XCode project, it won't build. How can I get XCode to build this simple program?
cx-config output:
XCode project settings:
EDIT 2: I exported the Build Log out of XCode and saw that the actual build command uses clang. When I successfully compile Hello World in the terminal, I use clang++. When I try the same command with clang, it doesn't work. Perhaps this is the issue, since clang only links C libraries and the errors XCode is giving me relate to not being able to find standard C++ functions.
EDIT 3: Wow I hate XCode. I got my Hello World program to compile by accident. The solution was putting the wxWidgets compiler flags in the Targets Build Settings, whereas before, I had it in the Project Build Settings. I assumed the Targets would inherit the Project settings, but I guess not!
EDIT 4: I got my complicated XCode project to compile with the wxWidgets libraries by taking all the search paths & flags and moving them from the Project Build Settings to the Targets Build Settings. What a nightmare! Glad it's over
#WaddleDee72
,
I suggest doing following:
Delete wxWidgets directory.
Unpack wxWidgets into i.e. ~/wxWidgets.
Open Terminal.
In the Terminal
4a. cd ~/wxWidgets
4b. mkdir buildOSX
4c. cd buildOSX
4d. ../configure --enable-debug
4e. make -j5
After successful build
wx-config --cxxflags
wx-config --libs
Use the output of 2 commands above and put the values where they belong in XCode project.
If you get any issues - let us know.
Thank you.

Using MySQL in XCODE for C++ project

Currently, I have a project in C++ where i have to get a mysql database and ask for tablevalues. I understand that now I have to import the mysql library.
So I do so by going to this site:
MySQL Mac OSX 10.7 (64bit)
Now that I have successfully downloaded the files and imported the include folder and libraries that the folder contains, I get this error in the mysql.driver.h file I imported with the rest of the mysql files in the folder I just recently downloaded ...
#include <boost/scoped_ptr.hpp> // 'boost/scoped_ptr.hpp' file not found
So this is telling me that this templated header file was not found...
Do I need to download the boost library now? I did and imported BOOST into my project but it just gave me more errors...
Anyone ran into this same issue before or can help? :) I'd greatly appreciate it!
EDIT
I also read up on a forum in the mySQL webpage and saw that I would need to add linker flags and header/library search paths? I did this but nothing helped :(
Header Search Paths - /usr/local/mysql/include
Library Search Paths - /usr/local/mysql/lib
Other Linker Flags -lz -lm -lmysqlclient
I am a newbie and had a hard time on getting the C++ connector for mysql to work under Xcode. These are the steps necessary for it to work:
Download the MySQL C connector dmg package and install it. This package installs in /usr/local/mysql-connector.... Rename that folder to mysql. (in order to view the /usr folder in finder click on the GO menu and then on GO TO FOLDER once there type in /usr.)
Download the MySQL C++ connector binary. Uncompress it by double clicking the file. Once the file uncompresses, create a folder named mysqlCPP in /usr/local/ (you will now have a folder named mysql and mysqlCPP). Now copy the folders /include and /lib from the uncompressed C++ connector folder to the mysqlCPP folder you just created.
Download the BOOST library from boost.org. Uncompress the downloaded file and copy the boost directory to /usr/local/. (now you should have /mysql /mysqlCPP /boost directories here. There may be additional system folders DONT DELETE THEM.
Now on XCODE open your project and in Build Settings do the following:
Look for OTHER LINKER FLAGS, double click and add the following: -lz -lm -lmysqlcppconn-static (this builds a static reference and a bigger executable, if you wish to link dynamically you need type -lz -lm -lmysqlcppconn but you MUST COPY THE dynamic library into the EXECUTION directory of the file. Add to all options under OTHER LINKER FLAGS.
Look for HEADER SEARCH PATHS, double click and add the following /usr/local/mysqlCPP/include, /usr/local/mysql/include and /usr/local/ all should be added non-recursive. Don't forget to type in for all inputs Debug, Release, Any Architecture, etc.
Look for LIBRARY SEARCH PATHS, double click and add the following: /usr/local/mysql/lib and /usr/local/mysqlCPP/lib. Don't forget to type in for all inputs Debug, Release, Any Architecture, etc. Now you have both installed Connector-C and Connector-C++.
Look for C++ Standard Library and choose the libstdc++ (GNU C++ standard library)
Now you should be set and ready to compile C++ programs. If you wish to use the C connector instead then you should change the OTHER LINKER FLAGS to: -lm -lz -lmysqlclient. Just try with any of the samples on mysql.com and both connectors should work.
The documentation clearly states boost is required for building the MySQL connector from source
As of MySQL Connector/C++ 1.1.0, the Boost C++ libraries 1.34.0 or
newer must be installed. Boost is required only to build the
connector,
Once you have boost installed, just point to its installation location
Once Boost is installed, tell the build system where the Boost files
are by defining the BOOST_ROOT:STRING option. This can be done when
you invoke CMake. For example:
shell> cmake . -DBOOST_ROOT:STRING=/usr/local/boost_1_40_0

How to include OpenCV include into XCode 4 c++ project?

I have seen posts on this before, but none of the answers seemed to work for me. I just installed OpenCV on my Mac and got it to work fine with g++ from the command line. I installed it using Mac Ports:
$ sudo port install opencv configure.compiler=llvm-gcc-4.2
To compile with g++, I use the command:
g++ myfile.cpp -o myprogram -I/opt/local/include -L/opt/local/lib -lopencv_core.2.4.2 -lopencv_calib3d.2.4.2
And my OpenCV include header in the main.cpp file is:
#include "opencv2/opencv.hpp"
And all works well.
Now, when I tried to include the library in Xcode, it simply says 'opencv2/opencv.hpp' file not found. To include the library in Xcode, I followed some detailed instructions. First, to the project target, I added /opt/local/lib to the library search paths, and /opt/local/include, /opt/local/include/opencv and /opt/local/include/opencv2 to the header search paths under the Build Settings tab. Then I clicked on Build Phases, and to Link Binary With Libraries I added all of the OpenCV .dylib files in /opt/local/lib (all 11 of them). This didn't work. I've tried many other things, including adding to user search paths and setting up "Other Linker Flags" for the target. Nothing worked. It can't find the file. If I use the exact path (#include "/opt/local/include/opencv2/opencv.hpp") it can't find all the other header files.
The version of OpenCV is 2.4.2. I am using Xcode 4.3.3 on OS X Lion 10.7.4. This is a C++ project. Any ideas?
This problem can be solved by going to the project properties and click on the target.
In the build settings search "header search path".
And there you add "/opt/local/include".
By doing this you're telling Xcode to look for the header files in that directory also.
Now you can use:
#include <opencv2/opencv.hpp>
If you don't want to do this every time you start a new project you can add the .h and .hpp files to the defaulti include directory Xcode uses. You can find this directory in Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include .
You will have to add the binaries to your project as well. For that you go to "build phases" and then add the binaries in the "Link binaries with libraries" section.
I hope this helps you out.

How can I get Xcode to link and debug an app with Boost Filesystem?

TL;DR
Objective-C app linked with static library that dynamic links Boost Filesystem. App can be run from output directory using Terminal, but trying to run from Xcode debugger or Finder gives error dyld: Library not loaded: libboost_filesystem.dylib <snip> Reason: image not found.
Problem
In my Xcode project I have a structure that looks like this:
MainProject (Objective-C)
- static_lib_that_uses_filesystem (C++)
To get everything to link, I added libboost_system and libboost_filesystem dylibs to the "Link Binary with Libraries" build phase in MainProject.
When I try to run the app from either Xcode or Finder I get:
sharedlibrary apply-load-rules all
warning: Unable to read symbols for libboost_filesystem.dylib (file not found).
warning: Unable to read symbols from "libboost_filesystem.dylib" (not yet mapped into memory).
warning: Unable to read symbols for libboost_system.dylib (file not found).
warning: Unable to read symbols from "libboost_system.dylib" (not yet mapped into memory).
[Switching to process 43957 thread 0x0]
dyld: Library not loaded: libboost_filesystem.dylib
Referenced from: /Users/ssteele/Library/Developer/Xcode/DerivedData/MainProject-dqrhyuarllykslftblocjdzxlran/Build/Products/Debug/MainProject.app/Contents/MacOS/MainProject
Reason: image not found
I added a build stage to copy the dylibs to the Frameworks directory in the bundle, this doesn't help. I changed this to copy them to the Executables directory which also didn't help.
Having them in the Executables directory does allow me to run the app from Terminal.
How can I get the app to find the dylibs when run from Finder/Xcode?
Background Info
I'm using Xcode 4.2 on Lion and currently targeting Lion only. I built my shared libraries for filesystem like this:
./b2 threading=multi macosx-version=10.7 --with-filesystem stage
This creates libboost_system.dylib, libboost_filesystem.dylib, and also .a equivalents in the stage/lib directory, I'm referencing them in the project directly from there.
Dynamic libs (dylibs) on OSX bake in the path that they should be loaded from. For example...
/usr/lib/some_awesome.dylib.
When you link to a dylib, the linker embeds this path in your executable as the place to look for it at runtime. This is fine and easy with installed libs, but for relative path linking it's more complicated.
When you build the boost libs, they just get their names embedded rather than a full or relative path (i.e. libboost_system.dylib rather than /usr/lib/libboost_filesystem.dylib). You should be able to change this with the dll-path option, but that seems broken currently.
To fix your problem you either need to get the correct path relative to your application embedded (e.g. #executable_path/libwhatever.dylib) into the dylibs somehow, which would probably require the bjam dll-path option to work, or instead you can fix your executable to look in a different location.
To do this, use something like the following as a script step in your build:
install_name_tool -change libboost_filesystem.dylib #executable_path/libboost_filesystem.dylib$BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH
Note that if you have multiple dylibs that reference each other with broken paths, you'll need to fix the paths between them too, e.g.
install_name_tool -change libboost_system.dylib #executable_path/libboost_system.dylib$BUILT_PRODUCTS_DIR/$EXECUTABLE_FOLDER_PATH/libboost_filesystem.dylib
The following is a good article on this: Creating working dylibs
The issue is that boost needs to be installed e.g. b2 ..... install. This copies the libraries and headers into /usr/local/lib and /usr/local/include.
OSX dynamic libraries only run from the directory that they are built for.
You can change the install directory by using the -prefix argument to boost build. However the libraries would still need to be in the same directory for all users.
There should be a way of building boost as a framework and/or embedding #executable_path in the library.
An alternative is to use boost's static libraries - build the static only or delete the dynamic ones, Xcode looks for dynamic before static. If using static then the path to the libraries does not matter at run time as all the code is now in the executable.
I think you need to add the path to the directory where you saved libboost_filesystem.a in the "library search paths"
Click on your project profile -> build settings -> expand "Search path" -> "library search paths"

Linking Libraries in Xcode

I'm using a powerbook (osx 10.5) and recently downloaded and installed FFTW 3.2 (link text). I've been able to compile and run some simple programs based on the online tutorial using the terminal:
g++ main.cpp -lfftw3 -lm
However, I can't get the same program to compile in Xcode. I get a linking error, "symbol(s) not found". There is a file called libfftw3.a in /usr/local/lib. How can this be linked? Furthermore, apparently the libraries have to be linked in a particular order, i.e. see: link text
thanks for any help
To link to a .a library like this in Xcode you need to:
Double-click on your target or application in Xcode to open the Info window
Switch to the "Build" tab
Add -lfftw3 to "Other Linker Flags" (under "Linking")
Add the path to your library to the "Library Search Paths" (under "Search Paths"). In your case this will be /usr/local/lib
We use FFTW (it's great by the way), this works for us!
Did you set these options for the target?
Under "Linking->Other Linker Flags" add: "-L/path/to/library -lfftw3 -lm"
warning in /Developer/SDKs/MacOSX10.5.sdk/usr/local/lib/libfftw3.a, file is not of required architecture
Maybe the binary format was different e.g. the library could be 32 bit while the application 64.
OK - I finally got this working. I also needed the the GNU Scientific Libraries and ran into similar issues with the architecture setting. For anyone interested, this tutorial goes through how to set up Xcode and link libraries:
https://web.archive.org/web/20101006023300/http://www.boyfarrell.com/learning-curve/gnu-scientific-library-and-xcode-31