在Ubuntu上执行编译文件:"无效或不完整的多字节或宽字符"以及有趣的UTF-8字符

时间:2014-04-08 14:57:33

标签: c++ opencv ubuntu compiler-construction exe

我试图在Ubuntu中编译并执行一个完美的.cpp文件。 我有Ubuntu 12.04和OpenCV 2.4.7。

我正在使用的文件是mergevec.cpp:mergevec.cpp code 为方便起见,我也在这里粘贴了代码:

#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "cvhaartraining.h"
#include "_cvhaartraining.h" // Load CvVecFile
// Write a vec header into the vec file (located at cvsamples.cpp)
void icvWriteVecHeader( FILE* file, int count, int width, int height );
// Write a sample image into file in the vec format (located at cvsamples.cpp)
void icvWriteVecSample( FILE* file, CvArr* sample );
// Append the body of the input vec to the ouput vec
void icvAppendVec( CvVecFile &in, CvVecFile &out, int *showsamples, int winwidth, int winheight );
// Merge vec files
void icvMergeVecs( char* infoname, const char* outvecname, int showsamples, int width, int height );

// Append the body of the input vec to the ouput vec
void icvAppendVec( CvVecFile &in, CvVecFile &out, int *showsamples, int winwidth, int winheight )
{
CvMat* sample;

if( *showsamples )
{
cvNamedWindow( "Sample", CV_WINDOW_AUTOSIZE );
}
if( !feof( in.input ) )
{
in.last = 0;
in.vector = (short*) cvAlloc( sizeof( *in.vector ) * in.vecsize );
if ( *showsamples )
{
if ( in.vecsize != winheight * winwidth )
{
fprintf( stderr, "ERROR: -show: the size of images inside of vec files does not match with %d x %d, but %d\n", winheight, winwidth, in.vecsize );
exit(1);
}
sample = cvCreateMat( winheight, winwidth, CV_8UC1 );
}
else
{
sample = cvCreateMat( in.vecsize, 1, CV_8UC1 );
}
for( int i = 0; i < in.count; i++ )
{
icvGetHaarTraininDataFromVecCallback( sample, &in );
icvWriteVecSample ( out.input, sample );
if( *showsamples )
{
cvShowImage( "Sample", sample );
if( cvWaitKey( 0 ) == 27 )
{
*showsamples = 0;
}
}
}
cvReleaseMat( &sample );
cvFree( (void**) &in.vector );
}
}

void icvMergeVecs( char* infoname, const char* outvecname, int showsamples, int width, int height )
{
char onevecname[PATH_MAX];
int i = 0;
int filenum = 0;
short tmp;
FILE *info;
CvVecFile outvec;
CvVecFile invec;
int prev_vecsize;

// fopen input and output file
info = fopen( infoname, "r" );
if ( info == NULL )
{
fprintf( stderr, "ERROR: Input file %s does not exist or not readable.\n", infoname );
exit(1);
}
outvec.input = fopen( outvecname, "wb" );
if ( outvec.input == NULL )
{
fprintf( stderr, "ERROR: Output file %s is not writable.\n", outvecname );
exit(1);
}

// Header
rewind( info );
outvec.count = 0;
for ( filenum = 0; ; filenum++ )
{
if ( fscanf( info, "%s", onevecname ) == EOF )
{
break;
}
invec.input = fopen( onevecname, "rb" );
if ( invec.input == NULL )
{
fprintf( stderr, "ERROR: Input file %s does not exist or not readable.\n", onevecname );
exit(1);
}
fread( &invec.count,   sizeof( invec.count )  , 1, invec.input );
fread( &invec.vecsize, sizeof( invec.vecsize ), 1, invec.input );
fread( &tmp, sizeof( tmp ), 1, invec.input );
fread( &tmp, sizeof( tmp ), 1, invec.input );

outvec.count += invec.count;
if( i > 0 &&  invec.vecsize != prev_vecsize )
{
fprintf( stderr, "ERROR: The size of images in %s(%d) is different with the previous vec file(%d).\n", onevecname, invec.vecsize, prev_vecsize );
exit(1);
}
prev_vecsize = invec.vecsize;
fclose( invec.input );
}
outvec.vecsize = invec.vecsize;
icvWriteVecHeader( outvec.input, outvec.count, outvec.vecsize, 1);

// Contents
rewind( info );
outvec.count = 0;
for ( i = 0; i < filenum ; i++ )
{
if (fscanf( info, "%s", onevecname ) == EOF) {
break;
}
invec.input = fopen( onevecname, "rb" );
fread( &invec.count,   sizeof( invec.count )  , 1, invec.input );
fread( &invec.vecsize, sizeof( invec.vecsize ), 1, invec.input );
fread( &tmp, sizeof( tmp ), 1, invec.input );
fread( &tmp, sizeof( tmp ), 1, invec.input );

icvAppendVec( invec, outvec, &showsamples, width, height );
fclose( invec.input );
}
fclose( outvec.input );
}

int main( int argc, char **argv )
{
int i;
char *infoname   = NULL;
char *outvecname = NULL;
int showsamples  = 0;
int width        = 24;
int height       = 24;

if( argc == 1 )
{
printf( "Usage: %s\n  <collection_file_of_vecs>\n"
"  <output_vec_filename>\n"
"  [-show] [-w <sample_width = %d>] [-h <sample_height = %d>]\n",
argv[0], width, height );
return 0;
}
for( i = 1; i < argc; ++i )
{
if( !strcmp( argv[i], "-show" ) )
{
showsamples = 1;
// width = atoi( argv[++i] ); // obsolete -show width height
// height = atoi( argv[++i] );
}
else if( !strcmp( argv[i], "-w" ) )
{
width = atoi( argv[++i] );
}
else if( !strcmp( argv[i], "-h" ) )
{
height = atoi( argv[++i] );
}
else if( argv[i][0] == '-' )
{
fprintf( stderr, "ERROR: The option %s does not exist. n", argv[i] );
exit(1);
}
else if( infoname == NULL )
{
infoname = argv[i];
}
else if( outvecname == NULL )
{
outvecname = argv[i];
}
}
if( infoname == NULL )
{
fprintf( stderr, "ERROR: No input file\n" );
exit(1);
}
if( outvecname == NULL )
{
fprintf( stderr, "ERROR: No output file\n" );
exit(1);
}
icvMergeVecs( infoname, outvecname, showsamples, width, height );
return 0;
}

当我使用:

编译它时
g++ `pkg-config --cflags opencv` -o mergevec mergevec.cpp cvboost.cpp cvcommon.cpp cvsamples.cpp cvhaarclassifier.cpp cvhaartraining.cpp `pkg-config --libs opencv`

有一些警告,但没有错误。 但当我这样运行时:

sudo sh ./mergevec samples.txt samples.vec

我收到这些错误:

./mergevec: 1: ./mergevec: cannot create �%@@8y@8: Invalid or incomplete multibyte or wide character
./mergevec: 1: ./mergevec: ELF: not found
./mergevec: 2: ./mergevec: �: not found
./mergevec: 1: ./mergevec: Syntax error: Unterminated quoted string

我认为文件编码可能存在问题,因此我使用UTF-8编码保存了mergevec.cpp,然后再次尝试使用Windows ISO编码,然后使用Unix / Linux行结尾保存,然后使用WIndows行结尾保存。我正在使用gedit。

我在做什么有什么不对?这个相同的代码在其他机器上编译和执行完全正常。我有一个在另一台机器上编译的可执行文件,并且在该机器上工作正常,但是当我在这里运行相同的.exe文件时,它也给出了同样的疯狂错误。

修改

我跑了:

file mergevec

我得到了:

mergevec: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xac5b49d6c194016722ea75ec6f8529578b45d9bc, not stripped

1 个答案:

答案 0 :(得分:1)

sudo sh ./mergevec samples.txt samples.vec
上面的

sh是一个shell。它期望它的第一个参数是一个shell脚本,一个文本文件。

你有一个已编译的可执行文件,所以正确的做法是直接运行它:

sudo ./mergevec samples.txt samples.vec
相关问题