C++ libPNG - simple initialisation error - c++

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!)

Related

develop with ffmpeg link failed [duplicate]

This question already has answers here:
gcc compiles libavformat code, but g++ does not [duplicate]
(1 answer)
Undefined reference, using FFMpeg-library (AvCodec) on Ubuntu, 64-bits system
(1 answer)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I write to test ffmpeg and call ffmpeg function, but it alwasy tell me link err: can not found ffmpeg function:
this is my code:
#ifdef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
#endif
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
#include <libavutil/imgutils.h>
#include <libavutil/samplefmt.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
static const char *src_filename = NULL;
static const char *video_dst_filename = NULL;
static const char *audio_dst_filename = NULL;
static AVFormatContext *fmt_ctx = NULL;
int main(int argc, char **argv) {
int ret = 0;
if (argc != 4) {
fprintf(stderr, "usage: %s input_file video_output_file audio_output_file\n"
"API example program to show how to read frames from an input file.\n"
"This program reads frames from a file, decodes them, and writes decoded\n"
"video frames to a rawvideo file named video_output_file, and decoded\n"
"audio frames to a rawaudio file named audio_output_file.\n",
argv[0]);
exit(1);
}
// src_filename = argv[1];
src_filename = "/tmp/a.mp4";
video_dst_filename = argv[2];
audio_dst_filename = argv[3];
/* open input file, and allocate format context */
if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
fprintf(stderr, "Could not open source file %s\n", src_filename);
exit(1);
}
avformat_close_input(&fmt_ctx);
}
the follow is build command:
clang++ -I /usr/local/apps/homebrew/Cellar/ffmpeg/4.4_1/include/ -L/usr/local/apps/homebrew/Cellar/ffmpeg/4.4_1/lib -lavformat main.cpp
and the terminal output:
Undefined symbols for architecture x86_64:
"avformat_open_input(AVFormatContext**, char const*, AVInputFormat*, AVDictionary**)", referenced from:
_main in main-f2a6f0.o
"avformat_close_input(AVFormatContext**)", referenced from:
_main in main-f2a6f0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
then Who can tell me why.

Is there something missing from this extremely basic but uncooperative code?

I recently started learning C++, and I'm currently trying to test out the header file functionality. I wrote three simple files:
headertest.h:
#pragma once
class Cube
{
public:
double getVolume();
double getSurfaceArea();
void setLength(double length);
private:
double length_;
};
headertest.cpp:
#include "headertest.h"
double Cube::getVolume()
{
return length_ * length_ * length_;
}
double Cube::getSurfaceArea()
{
return 6 * length_ * length_;
}
void Cube::setLength(double length)
{
length_ = length;
}
and main.cpp:
#include "headertest.h"
#include <iostream>
int main()
{
Cube c;
c.setLength(3.48);
double volume = c.getVolume();
std::cout << volume;
}
However, when I try to build the files, I get the following error:
> Executing task: /usr/bin/clang++ -std=c++17 -stdlib=libc++ -g /Users/jonathan/Desktop/myc++/cubetest/headertest.cpp -o /Users/jonathan/Desktop/myc++/cubetest/headertest <
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
Is there something wrong about how I wrote the main.cpp file, or am I leaving out something important? I've spent all day trying to figure out this issue, and I have no idea how to get unstuck. Thank you!!

Clang unable to link files in VS Code

I recently started learning C++, and I'm currently trying to test out the header file functionality. I wrote three simple files:
headertest.h:
#pragma once
class Cube
{
public:
double getVolume();
double getSurfaceArea();
void setLength(double length);
private:
double length_;
};
headertest.cpp:
#include "headertest.h"
double Cube::getVolume()
{
return length_ * length_ * length_;
}
double Cube::getSurfaceArea()
{
return 6 * length_ * length_;
}
void Cube::setLength(double length)
{
length_ = length;
}
and main.cpp:
#include "headertest.h"
#include <iostream>
int main()
{
Cube c;
c.setLength(3.48);
double volume = c.getVolume();
std::cout << volume;
}
However, when I try to build the files with Clang, I get the following error:
> Executing task: /usr/bin/clang++ -std=c++17 -stdlib=libc++ -g /Users/jonathan/Desktop/myc++/cubetest/headertest.cpp -o /Users/jonathan/Desktop/myc++/cubetest/headertest <
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
Does anyone have any idea of why this is happening? I've tried running g++ main.cpp headertest.cpp -o main before building, but the error isn't getting solved. Thank you!

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

Compiling libzip on Mac: Undefined symbols for architecture x86_64

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