Why can't I compile this command-line OpenCV Mac application? - xcode

Following is my step:
1)create a command line tool project "OpenCV"
2)add files to the project which are in /usr/local/lib with suffix 2.4.2, such as
"libopencv_calib3d.2.4.2.dylib"
3)add "/usr/local/include" to project's Header Search Path
4)type this program:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cvaux.hpp>
int main(int argc, char** argv)
{
IplImage * pInpImg = 0;
// Load an image from file - change this based on your image name
pInpImg = cvLoadImage("my_image.jpg", CV_LOAD_IMAGE_UNCHANGED);
if(!pInpImg)
{
fprintf(stderr, "failed to load input image\n");
return -1;
}
// Write the image to a file with a different name,
// using a different image format -- .png instead of .jpg
if( !cvSaveImage("my_image_copy.png", pInpImg) )
{
fprintf(stderr, "failed to write image file\n");
}
// Remember to free image memory after using it!
cvReleaseImage(&pInpImg);
return 0;
}
However, I get error :
ld: library not found for -lopencv_calib3d.2.4.2
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Where is the problem?
I'm using mountain lion and Xcode 4.4

You don't need to add the opencv libs to your project but you do need to link to the libraries and set the library search path. I was able to compile and run your program with these settings:
Search paths:
Link to libraries:

Related

Ruby's native header files are giving me an error

When I include in my native C extension for a gem that I am writing, I get the following error:
../../../../ext/my_file/my_file.c:4:42: error: unknown type name 'RUBY_T_FIXNUM'
Here is where I use it in my code:
#include <stdlib.h>
#include <ruby-2.3.0/ruby.h>
void my_file_init(const T_STRING prog, T_FIXNUM verbose) {}
I added a link in /usr/local/include to ~/.rvm/rubies/default/include/ruby-2.3.0, which is where I have the version of ruby that I want to use installed. The only definition I could find for the T_FIXNUM constant in the ruby header files was within an enum in ruby/ruby.h:
enum ruby_value_type {
...
RUBY_T_FIXNUM = 0x15
};
Here is where the ruby/ruby.h file defines the T_FIXNUM type:
#define T_FIXNUM RUBY_T_FIXNUM
Am I including files incorrectly? Do I have the wrong version of Ruby? I'm working on OSX and I use RVM to manage my rubies. I have added symbolic links for config.h in the x86_64-darwin16/ruby dir, as well as a link from the ruby dir to itself.

Undefined reference when linking with googletest

When i try to build googletest (and googlemock) 1.8.0 i get an undefined reference to MakeAndRegisterTestInfo when i try to link with libgtest.a. It works fine with version 1.7.0 with the same cmake/make setup. I guess i could use 1.7.0 but then i need to download and build gmock separately.
CMakeFiles/unittest.dir/test/test_led.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
test_led.cpp:(.text+0x23d): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/unittest] Error 1
make[1]: *** [CMakeFiles/unittest.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
Other symbols in the libgtest.a works perfectly fine (example ::testing::InitGoogleTest) but as soon as i try to add a test with the macro TEST_F i get this error.
This is my testcase setup:
#include "gtest/gtest.h"
namespace {
// The fixture for testing (used by TEST_F).
class Foo : public ::testing::Test {
protected:
Foo();
virtual ~Foo() {};
virtual void SetUp();
virtual void TearDown();
};
Foo::Foo() {
};
void Foo::SetUp()
{
};
void Foo::TearDown()
{
};
TEST_F(Foo, Init) {
};
} // namespace
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();
return ret;
}
Cmake google test snippet:
It downloads from a local folder. It builds and i get a libgtest.a file.
### Unit test ###
# Google test
ExternalProject_Add(EXT_googletest
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
DOWNLOAD_COMMAND ""
SOURCE_DIR ${CMAKE_SOURCE_DIR}/../external/googletest
BUILD_COMMAND make all
# Disable install step
INSTALL_COMMAND "")
# Create a libgtest target to be used as a dependency by test programs
set(LIBGTEST_STATIC ${CMAKE_CURRENT_BINARY_DIR}/.../googlemock/gtest/libgtest.a)
add_executable(unittest
"test/test_foo.cpp")
target_link_libraries(unittest main_app_lib
${LIBGTEST_STATIC}
${CMAKE_THREAD_LIBS_INIT})
NM output libgtest.a 1.8.0
user#system$ nm .../libgtest.a |grep MakeAndRegisterTestInfo
000000000000743a T _ZN7testing8internal23MakeAndRegisterTestInfoEPKcS2_S2_S2_NS0_12CodeLocationEPKvPFvvES7_PNS0_15TestFactoryBaseE
NM output libgtest.a 1.7.0 (working file)
user#system$ nm .../libgtest.a |grep MakeAndRegisterTestInfo
0000000000005968 T _ZN7testing8internal23MakeAndRegisterTestInfoEPKcS2_S2_S2_PKvPFvvES6_PNS0_15TestFactoryBaseE
Solved:
There was an old version of google test installed on build machine which headers were used by cmake.
#user1178014 answered his own question but since there are no posted answers I am writing it up:
If you downloaded the gtest source directly and used the make install from the downloaded gtest repository it may have installed header files under /usr/local/include/gtest. If you later use apt-get to install the libgtest-dev debian package it installs the header files under /usr/include/gtest. If the version installed from the debian package is newer, your Makefile can pick up the older header files from /usr/include and give you link errors even though you
are correctly linking the new libgtest.a archive.
The solution is to look for /usr/local/include/gtest and /usr/include/gtest to see if they both exist. If they do then delete the older directory. If /usr/include/gtest is the older directory, you may want to remove it by uninstalling the libgtest-dev package.

Error when trying to compile in Xcode with SDL

I am trying to compile a project with SDL in Xcode and get the error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
-u command line option
I have the SDL.framework include along with Cocoa.framework in the Link Binary with Libraries. I also have SDLMain.h and SDLMain.m in the project.
This is all my code:
#include "SDLMain.h"
#include <SDL/SDL.h>
int main(int argc, const char * argv[])
{
return 0;
}
int main has to look like this:
int main(int argc, char * argv[])
{
return 0;
}
get rid of the const
It tool a quite long time until I got SDL and Xcode running. So, don't care. :-)
I uploaded here a simple SDL template for Xcode 4.5 and Mac OS X 10.7 and 10.8 (also using OpenGL 3.2 Core Profile possible). Step by Step instructions:
Download SDL (at the moment version 1.2.15)
Open the downloaded .dmg file
copy the SDL.framework into /Library/Frameworks/
Done. You can use the Xcode template (you should see a red area):
Further details and an image on my Blog (only german, sorry).

Setting up SDL on Mac OS X Lion with Xcode 4

I've been trying to get a super simple SDL program to work. I'm using Mac OS X Lion. I've got SDL to work in Snow Leopard, but it doesn't seem to want to work in lion. So far I have this:
#include <iostream>
#include "SDL/SDL.h"
using namespace std;
/*
#ifdef main
# undef main
#endif
*/
int main( int argc, char* args[] )
{
SDL_Surface* hello = NULL;
SDL_Surface* screen = NULL;
SDL_Init( SDL_INIT_EVERYTHING );
screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
hello = SDL_LoadBMP( "hello.bmp" );
SDL_BlitSurface( hello, NULL, screen, NULL );
SDL_Flip( screen );
SDL_Delay( 2000 );
SDL_FreeSurface( hello );
SDL_Quit();
return 0;
}
When I try to compile this code (in Xcode 4.1) it gives me this error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
(maybe you meant: _SDL_main)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I uncomment the #ifdef stuff I have commented currently, the program compiles, but then receives SIGABRT on the SDL_SetVideoMode line. That commented stuff I have I just saw in another program, I'm not sure if I'm supposed to have it or not.
How am I supposed to get this working?
The SDL header files redefine main with a macro. This is in SDL_main.h:
#define main SDL_main
But this is fine. SDL provides its own main() function that then calls your version. So get rid of those defines, they are making it worse, not better.
If your project is Cocoa based, then you probably missed to include SDLmain.m in your project. This provides a Cocoa friendly main() function. If your project is native C++, then my guess is that you did not include all the SDL libs in your project, so the linker isn't seeing SDL's own main().
If you are using the SDL framework you simply need to add the files SDLMain.h and SDLMain.m to your project (assuming you already have added SDL.framework to your project).
You find these files in the "devel-lite" folder of the SDL diskimage, which you can download here: http://www.libsdl.org/release/SDL-1.2.15.dmg
These two files will give you a Cocoa-friendly main routine, so that your SDL application can be a well-behaving OS X application.
SDL.h does not include SDL_main.h
The first line in your file should be:
#include SDL_main.h
SDL_main redefines the main function and then does its own initialization the is required to get SDL working with OS X
When compiling, you will also need to link in libSDLmain in addition to libSDL
I had the same link time error as OP for a pure C++ & OpenGL app, and the solutions was to use the sample project from https://github.com/Ricket/HelloSDL
This made me add the Cocoa libraries but that would have been needed anyway since I was targeting the iPhone.

how to get kernel header file?

i'm trying to make new system calls in kernel 2.6.21
and one of the new system calls use 'sys_write' function,
but to use this function, i realized that i must install kernel header file according to 2.6.21 version.
but i can't find kernel header file(2.6.21)
i tried apt-get install linux-headers-'uname -r' , apt-cache search linux-headers-$(uname -r)
but i can't find anything..
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/syscalls.h>
#include <linux/fcntl.h>
#include <asm/uaccess.h>
int main(){
int fd;
int old_fs;
old_fs = get_fs();
set_fs(KERNEL_DS);
fd = sys_open("config.txt", O_RDONLY, 0);
if(fd >= 0)
sys_close(fd);
set_fs(old_fs);
return 0;
}
vv.c:2:24: error: linux/init.h: No such file or directory
vv.c:3:26: error: linux/module.h: No such file or directory
vv.c:4:28: error: linux/syscalls.h: No such file or directory
In file included from /usr/include/asm/fcntl.h:1,
from /usr/include/linux/fcntl.h:4,
from vv.c:5:
/usr/include/asm-generic/fcntl.h:120: error: expected specifier-qualifier-list before ‘off_t’
/usr/include/asm-generic/fcntl.h:143: error: expected specifier-qualifier-list before ‘loff_t’
vv.c:6:25: error: asm/uaccess.h: No such file or directory
vv.c: In function ‘main’:
vv.c:12: error: ‘KERNEL_DS’ undeclared (first use in this function)
vv.c:12: error: (Each undeclared identifier is reported only once
vv.c:12: error: for each function it appears in.)
additionally, above header files is what i want to.
how can i get the kernel header file?
please help me..
Did installing the headers complete? If so then they should be installed in /usr/include and the include directives you gave should just work.
Edit:
First run:
sudo apt-get update
To make sure you have the latest packages, then:
sudo apt-get install linux-headers-$(uname -r)
This is the one package that should contain all the kernel header files in Ubuntu.

Resources