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
Related
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.
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.
I've just installed level db on my mac with brew install leveldb, and I've got this sample code:
#include <assert.h>
#include <string.h>
#include <leveldb/db.h>
#include <iostream>
int main(){
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options,"/tmp/testdb", &db);
assert(status.ok());
//write key1,value1
std::string key="key";
std::string value = "value";
status = db->Put(leveldb::WriteOptions(), key,value);
assert(status.ok());
status = db->Get(leveldb::ReadOptions(), key, &value);
assert(status.ok());
std::cout<<value<<std::endl;
std::string key2 = "key2";
//move the value under key to key2
status = db->Put(leveldb::WriteOptions(),key2,value);
assert(status.ok());
status = db->Delete(leveldb::WriteOptions(), key);
assert(status.ok());
status = db->Get(leveldb::ReadOptions(),key2, &value);
assert(status.ok());
std::cout<<key2<<"==="<<value<<std::endl;
status = db->Get(leveldb::ReadOptions(),key, &value);
if(!status.ok()) std::cerr<<key<<" "<<status.ToString()<<std::endl;
else std::cout<<key<<"==="<<value<<std::endl;
delete db;
return 0;
}
I tried to compile and link it:
$ ls /usr/local/lib/libleveldb.a
/usr/local/lib/libleveldb.a
$ clang++ -o test test.cpp /usr/local/lib/libleveldb.a -lpthread -Iinclude
Undefined symbols for architecture x86_64:
"snappy::RawCompress(char const*, unsigned long, char*, unsigned long*)", referenced from:
leveldb::TableBuilder::WriteBlock(leveldb::BlockBuilder*, leveldb::BlockHandle*) in libleveldb.a(table_builder.o)
"snappy::RawUncompress(char const*, unsigned long, char*)", referenced from:
leveldb::ReadBlock(leveldb::RandomAccessFile*, leveldb::ReadOptions const&, leveldb::BlockHandle const&, leveldb::BlockContents*) in libleveldb.a(format.o)
"snappy::MaxCompressedLength(unsigned long)", referenced from:
leveldb::TableBuilder::WriteBlock(leveldb::BlockBuilder*, leveldb::BlockHandle*) in libleveldb.a(table_builder.o)
"snappy::GetUncompressedLength(char const*, unsigned long, unsigned long*)", referenced from:
leveldb::ReadBlock(leveldb::RandomAccessFile*, leveldb::ReadOptions const&, leveldb::BlockHandle const&, leveldb::BlockContents*) in libleveldb.a(format.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Is there anything wrong with my installation and compilation?
It looks like you also need to be linking snappy. I assume this is because leveldb is also using that library. So you'll want to download that an install it as either a static or shared library and link it using -lsnappy. Make sure you place the linker flag to snappy after the linker flag for leveldb so it can fill in the symbols that leveldb is missing.
I've been learning C++ and have decided to try to create a simple file reader using libzip on archive files (e.g. Word).
I’ve recently installed libzip on my Macbook using brew but I seem to keep on getting the following issue whenever I try to compile a program that uses libzip:
Undefined symbols for architecture x86_64:
"_zip_fopen", referenced from:
_main in main-918bfa.o
"_zip_open", referenced from:
_main in main-918bfa.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [a.exe] Error 1
The command I use to compile:
g++ -g main.cpp -std=c++11 -I/usr/local/Cellar/libzip/0.11.2/include -I/usr/local/Cellar/libzip/0.11.2/lib/libzip/include -L/usr/local/Cellar/libzip/0.11.2/lib -o ../a.exe
main.cpp:
#include <iostream>
#include <fstream>
#include <zip.h>
#include <zlib.h>
using namespace std;
int numArgs = 2;
int main(int argc, char** argv){
// Parse command line arguments
if(argc != numArgs){
std::cout << "Incorrect number of arguments provided.\n";
std::cout << "Command line syntax: fileReader.exe inputFile" << endl;
exit(0);
}
// Try out libzip functionality
std::string inputDocument(argv[1]);
int err = 0;
zip* z = zip_open(inputDocument.c_str(), 0, &err);
if(z == NULL) {
printf("Could not read docx file. Error code: %d", err);
exit(-1);
}
zip_file* contentTypes = zip_fopen(z, "[Content_Types].xml", ZIP_FL_UNCHANGED);
exit(0);
}
Doesn't look like your including the libzip library in the compilation command. Try adding -lzip to your g++ command
I am getting the following error when trying to compile....
Undefined symbols for architecture x86_64:
"_png_sig_cmp", referenced from:
RenderUtils::isValidPng(std::istream&) in RenderUtils.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
my code is as follows:
//called from here
ifstream s;
s.open("/Users/tmg06qyu/Desktop/texture_512.png", ios::binary);
if(!RenderUtils::isValidPng(s)){
throw 20;
}
//header
class RenderUtils{
public:
static bool isValidPng(std::istream &source);
};
//implementation
#include <iostream>
#include "RenderUtils.h"
#include "png.h"
#define PNGSIGSIZE 8
using namespace std;
bool RenderUtils::isValidPng(std::istream &source){
//Allocate a buffer of 8 bytes, where we can put the file signature.
png_byte pngsig[PNGSIGSIZE];
int is_png = 0;
//Read the 8 bytes from the stream into the sig buffer.
source.read((char*)pngsig, PNGSIGSIZE);
//Check if the read worked...
if (!source.good()) return false;
//Let LibPNG check the sig. If this function returns 0, everything is OK.
is_png = png_sig_cmp(pngsig, 0, PNGSIGSIZE);
return (is_png == 0);
}
My guess is that you built a 32-bit version of libpng, but now you are trying to link 64-bit code with it. Try file * or otool -L * to check (from memory)
Sorry everyone....stupid me. I needed to link against zlib.....note to self.....always read the readme....(well not always!)