Xcode adding new library - c++

I ran into a problem I just can't fix. The problem is Im trying to add a new library to xcode and never did it before. I don't really now if the problem is library related or me adding a new library to xcode.
The Library Im trying to add is tidylib (tidy-html5).
#include <tidy.h>
#include <tidybuffio.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char **argv )
{
const char* input = "<title>Foo</title><p>Foo!";
TidyBuffer output = {0};
TidyBuffer errbuf = {0};
int rc = -1;
Bool ok;
TidyDoc tdoc = tidyCreate(); // Initialize "document"
printf( "Tidying:\t%s\n", input );
ok = tidyOptSetBool( tdoc, TidyXhtmlOut, yes ); // Convert to XHTML
if ( ok )
rc = tidySetErrorBuffer( tdoc, &errbuf ); // Capture diagnostics
if ( rc >= 0 )
rc = tidyParseString( tdoc, input ); // Parse the input
if ( rc >= 0 )
rc = tidyCleanAndRepair( tdoc ); // Tidy it up!
if ( rc >= 0 )
rc = tidyRunDiagnostics( tdoc ); // Kvetch
if ( rc > 1 ) // If error, force output.
rc = ( tidyOptSetBool(tdoc, TidyForceOutput, yes) ? rc : -1 );
if ( rc >= 0 )
rc = tidySaveBuffer( tdoc, &output ); // Pretty Print
if ( rc >= 0 )
{
if ( rc > 0 )
printf( "\nDiagnostics:\n\n%s", errbuf.bp );
printf( "\nAnd here is the result:\n\n%s", output.bp );
}
else
printf( "A severe error (%d) occurred.\n", rc );
tidyBufFree( &output );
tidyBufFree( &errbuf );
tidyRelease( tdoc );
return rc;
}
My problem is that I get the following error all the time and I just can't fix it:
Undefined symbols for architecture x86_64:
"_tidyBufFree", referenced from:
_main in main.o
"_tidyCleanAndRepair", referenced from:
_main in main.o
"_tidyCreate", referenced from:
_main in main.o
"_tidyOptSetBool", referenced from:
_main in main.o
"_tidyParseString", referenced from:
_main in main.o
"_tidyRelease", referenced from:
_main in main.o
"_tidyRunDiagnostics", referenced from:
_main in main.o
"_tidySaveBuffer", referenced from:
_main in main.o
"_tidySetErrorBuffer", 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)
What I did?
I set up the header search path to "/usr/local/Cellar/tidy-html5/5.6.0/include/"
I set up the library search path to "/usr/local/Cellar/tidy-html5/5.6.0/lib/"
I also tried to set other link flags to "-ltidylib"
System?
MacOS High Sierra 10.13.4
XCode 9.4.1
Solution
You have to use "-ltidy" instead of "-ltidylib" under other Other Linker Flags

Select your target (typically the app you're building) and click on the Build Phases tab. Build phases are the different steps needed for building, such as checking dependencies, compiling, copying resources, etc. One of them is "Link Binary with Libraries", and it includes a list of libraries and frameworks that your project should link. Click on the + button to add a new item to the list, and choose the library you want to add. Then build.

Related

Runtime error when compiling Objective-C project using g++

I've made a really small project using Objective-C, and If I run it with the Xcode, It works really well.
But, I need to compile it using below command lines:
g++ Main.mm -o Main `gnustep-config --objc-flags` `gnustep-config --base-libs` -O2 -DONLINE_JUDGE -DBOJ
This command line is what the site I'm trying to upload my code to compile uses to compile Objective-C projects.
But, whenever I compile whit that command line, I get runtime errors for using NSMutableArray, NSSet and NSString.
Errors:
zsh: command not found: gnustep-config
zsh: command not found: gnustep-config
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_NSMutableArray", referenced from:
objc-class-ref in Main-49f6e3.o
"_OBJC_CLASS_$_NSSet", referenced from:
objc-class-ref in Main-49f6e3.o
"_OBJC_CLASS_$_NSString", referenced from:
objc-class-ref in Main-49f6e3.o
"___CFConstantStringClassReference", referenced from:
CFString in Main-49f6e3.o
"_objc_msgSend", referenced from:
_main in Main-49f6e3.o
"_objc_opt_new", referenced from:
_main in Main-49f6e3.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
And, here is my source code:
#import <Foundation/Foundation.h>
int main(void) {
int num = 0;
int answer = 0;
scanf("%i", &num);
NSMutableArray *cl = [NSMutableArray new];
for (int k = 0; k < num; k++) {
char str[4];
getchar();
scanf("%[^\n]s", str);
NSString *userInput = [NSString stringWithUTF8String:str];
if (userInput.length == 1) {
[cl removeObjectAtIndex:0];
} else {
[cl addObject:[userInput substringFromIndex:[userInput length] - 1]];
}
NSMutableArray *result = [NSMutableArray new];
for (int i = 1; i <= cl.count; i++) {
for (int j = 0; j <= cl.count-i; j++) {
[result addObject:[[cl subarrayWithRange:NSMakeRange(j, i)] componentsJoinedByString:#""]];
}
}
answer += [[NSSet setWithArray:result] allObjects].count;
}
printf("%d\n", answer%1000000007);
return 0;
}
Those are linker errors, not runtime errors.
You need to link against the objc library and Foundation framework. Try:
g++ Main.mm -lobjc -framework Foundation ...

Apple Mach-O Linker (Id) Error in Xcode 9.4.1

I am trying to use the MATLAB engine through C++ on Xcode 9.4.1 but I am getting an error: "Apple Mach-O Linker (Id) Error". I searched for the answer and found out that turning off the "Bitcode" might help. However, when I go to the Build Setting of Xcode, it is just not there. I highlight, that it is definitely not there, even if you search in the search bar. How can I turn it off, and if I can't, what can I do?
Undefined symbols for architecture x86_64:
"_engEvalString", referenced from:
_main in main.o
"_engOpen", referenced from:
_main in main.o
"_engPutVariable", referenced from:
_main in main.o
"_mxCreateDoubleMatrix_800", referenced from:
_main in main.o
"_mxGetPr_800", 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)
Here is the full code:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#define BUFSIZE 256
int main() {
Engine *ep ;
mxArray *Y = NULL, *result = NULL ;
char buffer[BUFSIZE];
double x[10] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,
8.0, 9.0};
if (!(ep = engOpen("\0"))) {
fprintf(stderr, "\nCan't start MATLAB engine\n");
return EXIT_FAILURE;
}
Y = mxCreateDoubleMatrix(1,10, mxREAL);
memcpy((void *)mxGetPr(Y), (void *)x, sizeof(x));
engPutVariable (ep, "Y", Y) ;
engEvalString(ep, "fx = Y.^2") ;
engEvalString(ep, "plot(Y,fx);");
engEvalString(ep, "f(x) = y^2") ;
engEvalString(ep, "xlabel('x');");
engEvalString(ep, "ylabel('y');");
printf("Hit return to continue\n\n");
fgetc(stdin);
return 0 ;
}
the error message
According to the error message and the link command shown in the screen grab, it is not linking to the MATLAB libraries. I don't know how to properly set up Xcode to do so.
The best you can do is follow the advice in the MATLAB documentation, which suggests using the mex command in MATLAB as follows:
mex -v -client engine <filename.cpp>
Replace <filename.cpp> with the actual source file name, of course. First change directory to the location of that source file, and the executable will be placed next to it.

how to build the c++ code with boost library

what I want to do is a c++ code which utilize boost library and do a simple RS232 communication. I got the code like following:
#include <boost/asio.hpp> // include boost
using namespace::boost::asio; // save tons of typing
#include <iostream>
using std::cin;
// These are the values our port needs to connect
#ifdef _WIN32
// windows uses com ports, this depends on what com port your cable is plugged in to.
const char *PORT = "COM3";
#else
// Mac OS ports
const char *PORT = "/dev/tty.usbserial";
#endif
// Note: all the following except BAUD are the exact same as the default values
serial_port_base::baud_rate BAUD(19200);
serial_port_base::character_size C_SIZE( 8 );
serial_port_base::flow_control FLOW( serial_port_base::flow_control::none );
serial_port_base::parity PARITY( serial_port_base::parity::none );
serial_port_base::stop_bits STOP( serial_port_base::stop_bits::one );
int main()
{
io_service io;
serial_port port( io, PORT );
port.set_option( BAUD );
port.set_option( C_SIZE );
port.set_option( FLOW );
port.set_option( PARITY );
port.set_option( STOP );
unsigned char command[1] = {0};
// read in user value to be sent to device
int input;
cin >> input;
// The cast will convert too big numbers into range.
while( input >= 0 )
{
// convert our read in number into the target data type
command[0] = static_cast<unsigned char>( input );
write( port, buffer( command, 1 ) );
// read in the next input value
cin >> input;
}
// all done sending commands
return 0;
}
and I am building the code with following command:
c++ -Iboost_1_64_0 -Lboost_1_64_0/libs/ -stdlib=libc++ PortConfig.cpp -o PortConfig
but the terminal keeps giving me error:
Undefined symbols for architecture x86_64:
"boost::system::system_category()", referenced from:
boost::asio::error::get_system_category() in PortConfig-2187c6.o
boost::system::error_code::error_code() in PortConfig-2187c6.o
___cxx_global_var_init.2 in PortConfig-2187c6.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in PortConfig-2187c6.o
___cxx_global_var_init.1 in PortConfig-2187c6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
could anyone help me on that? Thanks in advance.
The compiler option -Lboost_1_64_0/libs/ just tells the compiler to look in that directory for libraries. You still need to specify which libraries to link. According to the boost documentation you will need the boost_system library, so add -lboost_system to the compiler options.
The corrected compile command should look something like this
c++ -Iboost_1_64_0 -Lboost_1_64_0/libs/ -lboost_system -stdlib=libc++ PortConfig.cpp -o PortConfig

OpenCV "referenced from: _main in main.o" build error

I am trying to run the following program in Xcode;
#include <stdio.h>
#include <stdlib.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
int TrackbarInitValue = 0;
int TrackbarStopValue;
CvCapture *capture = NULL;
IplImage *frame;
void onTrackbarSlide(int position)
{
cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, position);
// cvShowImage("Trackbar", frame);
}
int main(int argc, char **argv)
{
cvNamedWindow("Trackbar", CV_WINDOW_AUTOSIZE);
if(argc < 2)
{
printf("Please specify the video name n");
exit(0);
}
capture = cvCreateFileCapture(argv[1]);
TrackbarStopValue = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
if(TrackbarStopValue != 0)
{
cvCreateTrackbar("Position", "Trackbar", &TrackbarInitValue, TrackbarStopValue, onTrackbarSlide);
}
while(1)
{
frame = cvQueryFrame(capture);
if(!frame) break;
int pos = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES);
cvSetTrackbarPos("Position", "Trackbar", pos);
cvShowImage("Trackbar", frame);
char c = cvWaitKey(33);
if( c == 27) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("Trackbar");
return 0;
}
And when I try to build, I get the following errors;
Ld build/Debug/TrackBarPlayer normal x86_64
cd "/Users/facebooth/C++ practice/OpenCV Practice/TrackBarPlayer"
setenv MACOSX_DEPLOYMENT_TARGET 10.6
/Developer/usr/bin/g++-4.2 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk "-L/Users/facebooth/C++ practice/OpenCV Practice/TrackBarPlayer/build/Debug" -L/opt/local/lib "-F/Users/facebooth/C++ practice/OpenCV Practice/TrackBarPlayer/build/Debug" -filelist "/Users/facebooth/C++ practice/OpenCV Practice/TrackBarPlayer/build/TrackBarPlayer.build/Debug/TrackBarPlayer.build/Objects-normal/x86_64/TrackBarPlayer.LinkFileList" -mmacosx-version-min=10.6 -o "/Users/facebooth/C++ practice/OpenCV Practice/TrackBarPlayer/build/Debug/TrackBarPlayer"
Undefined symbols:
"_cvCreateFileCapture", referenced from:
_main in main.o
"_cvSetCaptureProperty", referenced from:
onTrackbarSlide(int) in main.o
"_cvNamedWindow", referenced from:
_main in main.o
"_cvSetTrackbarPos", referenced from:
_main in main.o
"_cvCreateTrackbar", referenced from:
_main in main.o
"_cvShowImage", referenced from:
_main in main.o
"_cvQueryFrame", referenced from:
_main in main.o
"_cvDestroyWindow", referenced from:
_main in main.o
"_cvReleaseCapture", referenced from:
_main in main.o
"_cvWaitKey", referenced from:
_main in main.o
"_cvGetCaptureProperty", referenced from:
_main in main.o
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
I'm a complete newbie to programming, and after literally hours of Googling, all I have discovered is that it's a linking problem. If anyone could give me some guidance on how to fix it, or what these errors are even actually saying, it would be very much appreciated.
link libopencv_videoio.3.2.6.dylib

Undefined symbols for architecture x86_64:"_glutInit", referenced from:_main in main.o / Netbeans on Mac

My program is this Sierpinski Gasket. I'm using Netbeans on my MacBook Pro and I believe I have the libraries installed but maybe they are not linked correctly.
#include <iostream>
#include <stdio.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#include <OpenGL/glext.h>
void myinit(){
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,50.0,0.0,50.0);
glMatrixMode(GL_MODELVIEW);
}
void display(){
GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}};
int i, j, k;
int rand();
GLfloat p[2]={7.5,5.0};
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
for(k=0; k<5000; k++){
j=rand()*3;
p[0]=(p[0]+vertices[j][0])/2.0;
p[1]=(p[1]+vertices[j][1])/2.0;
glVertex2fv(p);
}
glEnd();
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Sierpinski Gasket");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
Here are the compilation errors:
Undefined symbols for architecture x86_64:
"_glutInit", referenced from:
_main in main.o
"_glutInitDisplayMode", referenced from:
_main in main.o
"_glutInitWindowSize", referenced from:
_main in main.o
"_glutInitWindowPosition", referenced from:
_main in main.o
"_glutCreateWindow", referenced from:
_main in main.o
"_glutDisplayFunc", referenced from:
_main in main.o
"_glutMainLoop", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-MacOSX/sierpinski] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
You need to link the GLUT framework. In Project Properties > Linker > Command Line > Aditional Options, specify
-framework GLUT
Slight correction in your code:
j = rand() % 3;
and not rand() * 3;. That gives seg fault for obvious reasons.
To complement #user55721's GLUT framework answer, in the context of installing JasPer.
The INSTALL doc mentions an arch specific cmake option to use native GLUT on Mac OS:
When building the JasPer software under Mac OSX, only the use of the
native framework for OpenGL is officially supported. If the Freeglut
library is installed on your system, you will need to ensure that the
native GLUT library (as opposed to the Freeglut library) is used by
the build process. This can be accomplished by adding an extra option
to the cmake command line that resembles the following:
-DGLUT_glut_LIBRARY=/System/Library/Frameworks/GLUT.framework