I'm trying to get into C++ programming, and I'm quite struggling against the little details. Right now, I'm trying to get the snippet below to work, and apparently it will compile, but not link. (error message is a the bottom of this post)
I'm using libsndfile to open audio files, and the linker doesn't seem to find the corrent library files for it. This is on OS X 10.5.
I've downloaded libsndfile from mega-nerd.com and installed it via the ususal configure, make, sudo make install procedure. Is there anything else I need to do?
Edit: I have macports installed on my system, if that's of any concern.
Here's my code:
#include <iostream>
#include <string>
#include <sndfile.h>
#include "stereoSplit.h"
using namespace std;
int stereoSplit(string filename)
{
cout << filename;
SF_INFO sfinfo;
sfinfo.format = 0;
SNDFILE * soundfile;
soundfile = sf_open( filename.c_str(), SFM_READ, &sfinfo );
return 0;
}
int main (int argc, char const *argv[])
{
return stereoSplit("testStereo.wav");
}
And here's the complete error message:
Undefined symbols:
"_sf_open", referenced from:
stereoSplit(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in cc3hvkkk.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
You need to link in the library. Your compiler command should look something like:
g++ mystuff.cpp -lsndfile
To link the library in Xcode, select the project target, add a new item to Link binary with libraries, and find the libsndfile.1.dynlib on your computer.
Related
A beginner to armadillo library and have two problems with it.
The first question is about the link of armadillo and c++. I installed armadillo by following the instructions in the package and the location is /usr/include/, I could successfully run the code:
#include <iostream>
#include "/usr/include/armadillo"
using namespace std;
int main()
{
arma::mat a = arma::randu<arma::mat>(3, 3);
cout << a << endl;
return 0;
}
but if I want to define a function, for example, the inverse of the matrix (there is a function I can call from the library but I just want to do a test).
#include <iostream>
#include "/usr/include/armadillo"
using namespace std;
arma::mat Inverse( arma::mat A){
return arma::inv(A);
}
int main()
{
arma::mat a = arma::randu<arma::mat>(3, 3);
arma::mat inv_a = Inverse(a);
cout << inv_a << endl;
return 0;
}
then the program failed with three error messages.
Undefined symbols for architecture x86_64:
"_wrapper_dgetrf_", referenced from:
void arma::lapack::getrf<double>(int*, int*, double*, int*, int*, int*) in main.o
"_wrapper_dgetri_", referenced from:
void arma::lapack::getri<double>(int*, double*, int*, int*, double*, int*, int*) 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 checked by google and have tried some advices, like:" In miscellaneous MAC OS X linkers, I had to put "-01 -larmadillo" AND I had to deactivate the shared library settings!", to be honest, I can not find the miscellaneous option and deactivate the shared library settings as i am not familiar with Xcode. Then I followed install MacPorts and the library is installed to the folder /usr/local/include/, and I tried again, the first program is alright but the second program still has three errors, could anyone help me with this?
The second question would be the use of lapack when I am using armadillo in the same program, as I know armadillo is based on lapack, then if I declared the library armadillo, do I need to declare lapacke.h. Why do I ask this questions is that when I declared RcppArmadillo and lapacke.h, an error exists:
This file includes at least one deprecated or antiquated header. Please consider using of the 32 header found in section 17.4.1.2 of the C++ standard. Example include substituting the <X> header for the <X.h> hear for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated. [-W#warnings]
Line 11763 conflicting type for 'cgetrf'
Line 11765 conflicting type for 'zgetrg_'
...
I had the same problem, but eventually I solved it.
I didn't installed armadillo for my own purpose, but provided absolute path to include and library directories. Also, in a README there is a note that if you're on Mac then you should use Accelerate framework like this "-framework Accelerate". So after doing this all linker errors were gone.
I thought Ubuntu and OS X would have similar interfaces for compiling a C/C++ program with SDL but nothing I am trying, or finding on Google seems to be working.
I have found a solution for Xcode, but I am not using Xcode. I am writing my programs from Sublime Text 2 and compiling via command-line since I prefer it and work much faster that way.
Long story short, I am receiving an array of errors with each attempt and thus far I have copied SDL.framework into my /Library/Frameworks directory.
This is the closest I have gotten to actually compiling:
[ 674 / 174 / 0 ] $ compcpsdl
Undefined symbols for architecture x86_64:
"_SDL_main", referenced from:
-[SDLMain applicationDidFinishLaunching:] in ccYYA0Ea.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Terminal command (i.e. compcpsdl)
g++ -I/Library/Frameworks/SDL.framework/Headers main.cpp SDLmain.m -framework SDL -framework Cocoa
With the following project structure:
Project-Directory/
--- main.cpp
--- SDLMain.m
--- SDLMain.h
And lastly, the following source code:
#include <iostream>
#include <SDL/SDL.h>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
It appears I'm doing everything correctly, yet nothing seems to be working.
Any ideas guys?
I Think SDL does sometimes a bit strange stuff with the main function it assumes the mainfunction is defined in one way. Notice that I added int argc and char** argv to the definition of you main function.
Try:
int main (int argc, char** argv)
{
cout << "Hello World!";
return 0;
}
I encountered this problem also once and it was quite mystifying.
for more info see this stackoverflow question: Why SDL defines main macro?
I am developing for OSX 10.8. I just installed libpcap via MacPorts and tried running a simple device hunter (below)
#include <stdio.h>
#include <pcap.h>
int main(int argc,char *argv[])
{
char *dev, errbuf[PCAP_ERRBUF_SIZE];
dev = pcap_lookupdev(errbuf);
if(dev == NULL)
{
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
return(2);
}
printf("Device %s\n", dev);
return(0);
}
and while trying to compile with g++ i am getting:
Undefined symbols for architecture x86_64:
"_pcap_lookupdev", referenced from:
_main in ccIMp1m2.o
Any helpful advice so I can actually get started with learning this stuff would be great! I googled for a solid 10-15 minutes but just couldn't find much on my particular issue with my setup.
You need to link libpcap. Probably -lpcap added to your compiler command line will work. If it's installed somewhere strange (and it might be, since you got it from MacPorts), you might need -L/path/to/libpcap -lpcap.
I am having trouble using the SDL framework in my xcode project, my main (all there is in the project at the moment) currently looks like this:
#include <SDL/SDL.h>
#include <iostream>
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
And the error I am receiving when building is:
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
(maybe you meant: __Z8SDL_mainiPPKc)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
It does not give me an error about the framework not being found. and I have looked around Stackoverflow for this problem and I did find one person who had their framework in the wrong place on their OS. But I have tried all places (/library/frameworks && /~username~/library/frameworks && /system/library/frameworks) and still no luck.
Additional info:
I did however notice after some searching on the internet that the official website http://www.libsdl.org/download-1.2.php
Does not have a OSX version of the Development library. While many tutorials on how to use SDL on OSX say these are required.
Also I am adding my library via Xcode itself ,not through drag'ndrop. Xcode seems to reckognize it.
Would be much appreciated if anyone could help, going crazy over this error.
UPDATE:
Still no luck, I have followed every step provided by solutions here below. perhaps this screenshot is of any help.
The main() function is corrected, but didn't help. I tried changing the path to the header files, im lost on this one. Does anyone perhaps have any alternatives to building this in xcode?
UPDATE2:
The suggestion worked, however now it is giving me this warning, which won't let me run the application.
Fixed! I removed the path in the build settings. Strange how I still don't know what went wrong, either way. thanks a lot for the help! made my day!
I see three possible problems. The first problem is how you're including SDL.h in your code. You should include SDL.h with the following code:
#include "SDL.h"
The second possible problem is you haven't added the files SDLMain.h and SDLMain.m to your project. Those files are necessary to compile SDL code on Mac OS X. The files are in the devel-lite folder on the SDL disk image.
The third possible problem is that your project doesn't link to the Cocoa framework. The Mac version of SDL uses Cocoa so you need the Cocoa framework in your project.
The following article walks you through the setup of an Xcode 4 project for SDL:
Using SDL with Xcode 4
UPDATE
I noticed a possible problem in how you defined the main() function. You have a space between char and * and another space between * and argv, which could be causing the error. The main() function in my SDL code is defined in the following way:
int main(int argc, char *argv[])
I know it's a stupid question, but still.
I want to use the QJson library in my project. I've downloaded the tarball from the official site (probably need to mention that I'm under 64-bit Ubuntu 12.04). The INSTALL file has the following instructions
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=_preferred_path_ ..
make
make install
/sbin/ldconfig, if necessary
which I followed precisely. I've got the /include, /lib and share folders of QJson added to my /usr/local.
After that, I open my IDE (which is QtCreator), and make a test project with the following simple code:
#include <QVariant>
#include <qjson/serializer.h>
int main(int argc, char *argv[])
{
QJson::Serializer s;
QVariantMap map;
map["hello"] = QVariantList() <<"t1"<<"t2";
QByteArray json = s.serialize(map);
}
The #include is handled fine, all the types are recognized, the auto-complete for the QJson classes works fine. However, when trying to compile, I'm getting this (full path removed for readability):
<...>/QJsonTest/main.cpp:15: undefined reference to `QJson::Serializer::Serializer()'
<...>/QJsonTest/main.cpp:18: undefined reference to `QJson::Serializer::serialize(QVariant const&)'
<...>/QJsonTest/main.cpp:18: undefined reference to `QJson::Serializer::~Serializer()'
<...>/QJsonTest/main.cpp:18: undefined reference to `QJson::Serializer::~Serializer()'
collect2: ld returned 1 exit status
What's the reason, and how do I make it work?
You are hitting linker (not compiler) errors. You need to add the path to your QJson library files with a -L option