how to use object-Cpp in Xcode - c++

I created a new cocoa application in Xcode with 3 *.m files (main.m, AppDelegate.m, and projectnameTests.m).
In order to to use object-cpp, I renamed the 3 *.m files to *.mm files.
Then I get this following error from Xcode:
"Undefined symbols for architecture x86_64:
"_NSApplicationMain", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)"
I have never dealt .o files, so am not sure what's wrong.
Please help.
P.S.: I use the latest Xcode, version(6.1.1).

Add AppKit.framework to the "Link Binary With Libraries" section under the Build Phases for your project target, and that should fix the problem.
P.S. Don't ask me why XCode automatically finds the necessary framework (i.e. AppKit.framework) for .m files but not for .mm files, but adding AppKit.framework fixes the issue because NSApplicationMain is defined in AppKit. Adding Cocoa.framework also fixes the issue, likely because it triggers XCode to automatically link with AppKit.framework.

Related

vscode g++ Link failure : Undefined symbols for architecture x86_64

Basic Info:
system: macOS High Sierra(10.13.6)
editor : vs code(latest version)
Compiler: g++ (Xcode)
Target:deploy GLFW + GLAD
Question Description:
Recently, I'm learning to do some Computer Graphics related work. Everything is going smooth. However, when i create a window to test the env.Link error happened:
Undefined symbols for architecture x86_64:
"_gladLoadGLLoader", referenced from:
_main in main-5c211c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The terminal process terminated with exit code: 1
It seems I have not link some third party file. I
have fixed exactly the same problem by add "-lglfw" args to g++ when using functions in glfw3.h.
But when meeting glad related function : gladLoadGLLoader, I don't know how to do.
Something I have done:
Can find the head file.
#include glad/glad.h
#include GLFW/glfw3.h
Have put the file "glad.c" in workspace.
Try to Add "g++ -framework XXXXX" , But doesn't work.
Try to Add "g++ -lglfw3", But doesn't work.
Add "g++ -L or I /usr/lib or /usr/local/lib or /usr/local/include", But doesn't work.
Just tell the g++ to compile glad.c by adding "glad.c" to args. I thought glad.c will be compiled by default. Although I am not clear what happened, the problem is resolved anyway.
Add glad.c into Build Phases->Compile Sources

Setting C++ compile flags in xcode

I faced with the same issue for this question: Undefine symbols for architecture x86_64 using FFTW
And I tried to use flag -L and -l for C++ in xcode, but it doesn't work
Here is the error log:
clang: warning: -lsndfile: 'linker' input unused
clang: warning: -lfftw3: 'linker' input unused
clang: warning: argument unused during compilation: '-L/usr/local/lib'
Undefined symbols for architecture x86_64:
"_fftw_destroy_plan", referenced from:
_main in main.o
"_fftw_execute", referenced from:
_main in main.o
"_fftw_plan_dft_r2c_1d", referenced from:
_main in main.o
"_sf_close", referenced from:
_main in main.o
"_sf_open", referenced from:
_main in main.o
"_sf_read_double", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
But if I compile with gcc in command line, it works well.
gcc -I/Users/sr2/Documents/Soft/fftw-3.3.4 -I/usr/local/include
-L/usr/local/lib -lfftw3 -lsndfile main.c -o fft_sample
where am I wrong?
Instead of putting these under "Other C/C++ Flags", they should go under "Other Linker Flags" (in the Linking section).
(Note that my XCode is old, so it may be slightly different for your version.)
You might wonder, why is this necessary?
Well, when you build your project, there are several stages to go through. The most basic breakdown is into compiling and linking. (They could perhaps be broken down further, but that's the important distinction here.)
The compiler takes a source file (eg, example.cpp) and outputs an object file (such as example.o). An object file is not executable. When compiling, the compiler generally only knows about the one source file that it's currently processing. Thus the compiler doesn't need to know which libraries you're using - all it needs to know is where the header files are.
The linker takes one or more object files and combines them together to create an executable binary. At this point, it must also resolve any external symbols not defined in your code - for example, symbols defined in an external library. For that reason, the linker needs to know about any libraries you're using.
The compiler does not know what to do with an -l or -L flag - they're not relevant to the process of compiling your code into an object file.
When you invoke gcc from the command-line like you demonstrated, it automatically invokes the linker for you and forwards those -l and -L flags to it. Because of this, no object file is produced on disk, and you get an executable file.
However, when you build through XCode, it does things a little differently. It invokes the compiler once for each of your source files, producing an object file like I described above. (This is the reason why you can specify extra compiler flags for specific source files in the Build Phases -> Compile Sources section.) Because the compiler has been asked to produce an object file, it does not invoke the linker, and since you're trying to pass it flags that should be forwarded to the linker, you get that warning that the flags are not used.
Once all the source files have successfully compiled, XCode next invokes the linker directly to combine them all into a single executable binary. This is the stage that needs to know about your libraries. (Incidentally, in any large project, this method is generally preferable even if you're not using XCode.)
You need probably add
-lstdc++
to the Other Linker Flags in Build Settings of your Project.

Linker Flag/Boost not found -- linking Boost filesystem with Xcode

I am trying to the use Boost Filesystem library in Xcode.
I installed Boost with brew install boost, and it was successful.
Turns out Boost wasn't linked so I ran brew link --overwrite Boost, which worked.
I am trying to #include <boost/filesystem.hpp> in my project, but have not been successful so far.
At first Xcode couldn't find <boost/filesystem.hpp>, so I added
/usr/local/lib
/usr/local/include
to my Header search paths for Target and Project.
That fixed the original problem, but then boost_system could not be found by Xcode.
Here is the transcript:
Undefined symbols for architecture x86_64:
"boost::system::system_category()", referenced from:
___cxx_global_var_init2 in main.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in main.o
___cxx_global_var_init1 in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I added the -lboost_system linker flag to my project, and now I am getting this error:
ld: library not found for -lboost_system
clang: error: linker command failed with exit code 1 (use -v to see invocation)
And it's definitely the correct linker flag, so perhaps Boost installed incorrectly after all.
I will update this question if I find a solution, but I would appreciate help in solving this problem.
You only need /usr/local/include in your header search path and you need /usr/local/lib in your library search path in order for the linker to find the libraries.
Had the same problem in xcode 6 . user header search path and library search was okay . still showed link error . I solved the problem by adding -lboost_system-mt in other linker flag in project target setting

sql::mysql::get_driver_instance() Symbol not found

I'm trying to link the mysql native API to my server software now... With little luck. When compiling in Xcode on OSX10.9 using the flags
$(OTHER_CFLAGS) -I /usr/local/boost_1_55_0/ -I
/usr/local/mysqlconn/include/ -I /usr/local/mysqlconn/lib/
I get the compiler/linker error:
Undefined symbols for architecture x86_64:
"sql::mysql::get_driver_instance()", referenced from:
sql::mysql::get_mysql_driver_instance() in SQLComm.o ld: symbol(s) not found for architecture x86_64 clang: error: linker
command failed with exit code 1 (use -v to see invocation)
I really have no idea why though. The API files were downloaded by me today, and extracted at the included paths. I include all the files from include and include/cppconn in the server program that uses the function the linker cannot found.
Any ideas what I am doing wrong?
I see you configuring include directories, but I don't see any libraries listed there.
You need to link in the library when you build your project.

C++ Compilation Issue - Undefined symbols for architecture x86_64 - Mac Os X Mountain Lion

I'm having a compilation issue which I'm unable to solve. I'm developing a cross platform C++ project coding on both Mac Os X 10.8 and Windows. The code compiles and run fine on Windows and on Mac Os X Leopard as well.
Since Apple pushes the developers to stick to the latest platform for various reasons I'm forced to develop on Mountain Lion and I'm trying to get the project to work again.
I compiled correctly all the libraries I needed (wxWidgets, etc) and I imported the project in the latest version of Eclipse. When I try to build the project it tries to compile the firts .cpp file and at the end it (why?) tries to invoke the linker resulting on a series of missing symbols for my own defined classes. None of the other .cpp files is being compiled, so it's pretty understendable why the whole process is failing.
I also tried to invoke make from CLI, with the same result. I went into the makefile and everything seems correct. It looks like a very newbie issue, I feel I'm missing something huge here.
I'm pasting below the output of the compiler, just in case some compiling guru step in. Please feel free to ask for more details.
Compiler output
Pastebin Link: Compiler output
I used PB since the output is quite large.
The relevant section are the invocation of as and collect2 soon after the compiling phase of the very first .cpp file. The missing symbols are defined in other .cpp files in the same dir.
/usr/llvm-gcc-4.2/bin/../libexec/gcc/i686-apple-darwin11/4.2.1/as -arch x86_64 -force_cpusubtype_ALL -o /var/folders/br/h6ln_j014ll56zwc8x6xjmk80000gn/T//ccSUmHal.o /var/folders/br/h6ln_j014ll56zwc8x6xjmk80000gn/T//ccn8ex81.s
/usr/llvm-gcc-4.2/bin/../libexec/gcc/i686-apple-darwin11/4.2.1/collect2 -dynamic -arch x86_64 -macosx_version_min 10.8.3 -weak_reference_mismatches non-weak -o Calcoli.o -lcrt1.10.6.o -L/usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/x86_64 -L/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/x86_64 -L/usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1 -L/usr/llvm-gcc-4.2/bin/../lib/gcc -L/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1 -L/usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/../../.. -L/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/../../.. /var/folders/br/h6ln_j014ll56zwc8x6xjmk80000gn/T//ccSUmHal.o -lstdc++ -lSystem -lgcc -lSystem
The compiler output ends with the "classic" undefined symbol issue. All emphasized text*emphasized text*emphasized text
Undefined symbols for architecture x86_64:
"typeinfo for TipoPuntoCalc", referenced from:
Calcoli::setPuntoS(GTGraphicObject*) in ccSUmHal.o
"typeinfo for TipoPali", referenced from:
Calcoli::setPaloS(GTGraphicObject*) in ccSUmHal.o
"typeinfo for TipoRett", referenced from:
Calcoli::setFondazioneS(GTGraphicObject*) in ccSUmHal.o
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [Calcoli.o] Error 1
This shouldn't be an architecture related issue, since specifing i386 as target has the same result (symbol(s) not found for architecture i386).
Thank you,
Evelina
Go to your target's "Build Phases" section and verify that all the files you need to compile and link are actually included in the proper sections.
It sounds as if the compiler is not being told to include some things you need.