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

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

Related

glfw-3.3.5 - Undefined symbols for architecture arm64

Having linked all the libraries in cmake and wrtten code that'd open a window with GLFW, running build with the make command returns such output:
[main] Building folder: game
[build] Starting build
[proc] Executing command: /opt/homebrew/bin/cmake --build /Users/matt/projects/game/build --config Debug --target all -j 10 --
[build] Consolidate compiler generated dependencies of target game
[build] [ 50%] Building CXX object CMakeFiles/game.dir/main.cpp.o
[build] [100%] Linking CXX executable game
[build] Undefined symbols for architecture arm64:
[build] "_glClear", referenced from:
[build] _main in main.cpp.o
[build] "_glewExperimental", referenced from:
[build] _main in main.cpp.o
[build] "_glewInit", referenced from:
[build] _main in main.cpp.o
[build] "_glfwCreateWindow", referenced from:
[build] _main in main.cpp.o
[build] "_glfwGetKey", referenced from:
[build] _main in main.cpp.o
[build] "_glfwInit", referenced from:
[build] _main in main.cpp.o
[build] "_glfwMakeContextCurrent", referenced from:
[build] _main in main.cpp.o
[build] "_glfwPollEvents", referenced from:
[build] _main in main.cpp.o
[build] "_glfwSetInputMode", referenced from:
[build] _main in main.cpp.o
[build] "_glfwSwapBuffers", referenced from:
[build] _main in main.cpp.o
[build] "_glfwTerminate", referenced from:
[build] _main in main.cpp.o
[build] "_glfwWindowHint", referenced from:
[build] _main in main.cpp.o
[build] "_glfwWindowShouldClose", referenced from:
[build] _main in main.cpp.o
[build] ld: symbol(s) not found for architecture arm64
[build] collect2: error: ld returned 1 exit status
[build] make[2]: *** [game] Error 1
[build] make[1]: *** [CMakeFiles/game.dir/all] Error 2
[build] make: *** [all] Error 2
A thing to be noted is that it builds successfully with only the imports, but not the code responsible for opening a GLFW window, so it doesn't seem to be a problem with the linker.
My CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.12)
project(Game VERSION 1.0.0)
include_directories(${PROJECT_SOURCE_DIR}/deps/glfw-3.3.5/include/GLFW)
#---------------Configure dependency directories--------------------------------------------------------------
SET (PROJECT_DEPS "${PROJECT_SOURCE_DIR}/deps")
SET (glew_inc "${PROJECT_DEPS}/glew-2.1.0/include/GL/")
SET (glew_src "${PROJECT_DEPS}/glew-2.1.0/src/")
SET (glfw_inc "${PROJECT_DEPS}/glfw-3.3.5/include/GLFW/")
SET (glfw_src "${PROJECT_DEPS}/glfw-3.3.5/src/")
SET (glm "${PROJECT_DEPS}/glm/glm/")
#---------------Configure libraries----------------------------------------------------------------------------
include_directories(
${PROJECT_SOURCE_DIR}
${PROJECT_BINARY_DIR}
${glm}
${glew_inc}
${glew_src}
${glfw_inc}
${glfw_src}
${PROJECT_SOURCE_DIR}
)
#---------------Add executable---------------------------------------------------------------------------------
add_executable(game ${PROJECT_SOURCE_DIR}/main.cpp)
link_libraries(Game PRIVATE glfw-3.3.5)
link_libraries(Game PRIVATE glew-2.1.0)
link_libraries(Game PRIVATE glm)
And my main.cpp project root file, from which the executable is built looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <glew.h>
#include <glfw3native.h>
#include <glfw3.h>
#include <glm.hpp>
int main () {
glewExperimental = true; // Needed for core profile
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL
GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
do {
glClear( GL_COLOR_BUFFER_BIT );
glfwSwapBuffers(window);
glfwPollEvents();
} while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);
return 0;
}
I am using a MacBook Pro 2020 with a M1 chip.
link_libraries should be target_link_libraries. link_libraries only applies to targets defined after it

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.

Xcode adding new library

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.

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