如何使用libjpeg库构建C ++程序

时间:2013-11-16 15:51:25

标签: c++ gcc libjpeg

我想用libjpeg库构建C ++程序,但是当我尝试构建它时,我会收到以下错误。

g++ main.cpp -o main.o
main.cpp:26:22: warning: conversion from string literal to 'char *' is deprecated [-    Wdeprecated-writable-strings]
char *filename = "output.jpg";
                 ^
1 warning generated.
Undefined symbols for architecture x86_64:
  "_jpeg_CreateCompress", referenced from:
      _main in main-sUpMU6.o
  "_jpeg_destroy_compress", referenced from:
      _main in main-sUpMU6.o
  "_jpeg_finish_compress", referenced from:
      _main in main-sUpMU6.o
  "_jpeg_set_defaults", referenced from:
      _main in main-sUpMU6.o
  "_jpeg_set_quality", referenced from:
      _main in main-sUpMU6.o
  "_jpeg_start_compress", referenced from:
      _main in main-sUpMU6.o
  "_jpeg_std_error", referenced from:
      _main in main-sUpMU6.o
  "_jpeg_stdio_dest", referenced from:
      _main in main-sUpMU6.o
  "_jpeg_write_scanlines", referenced from:
      _main in main-sUpMU6.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: *** [main.o] Error 1

我的项目结构是这样的。

./myCodes
  main.h main.cpp Makefile
./jpeg-9
  libjpeg src and build outputs

制作文件是这样的。

ulibjpeg: main.o
    g++ -v -L../jpeg-9/cjpeg -L../jpeg-9/djpeg -L../jpeg-9 -l../jpeg-9 -ljpeg main.o -o     ulibjpeg

main.o: main.cpp
    g++ main.cpp -o main.o

我想我必须添加libjpeg库,但是我必须添加哪些文件,如何在g ++选项中添加库?

这是./jpeg-9目录的文件。

Makefile    jccoefct.c  jcprepct.lo jdmarker.o  jmemmgr.lo  makefile.bcc    rdjpgcom.1
Makefile.am jccoefct.lo jcprepct.o  jdmaster.c  jmemmgr.o   makefile.dj rdjpgcom.c
Makefile.in jccoefct.o  jcsample.c  jdmaster.lo jmemname.c  makefile.manx   rdjpgcom.o
README      jccolor.c   jcsample.lo jdmaster.o  jmemnobs.c  makefile.mc6    rdppm.c
aclocal.m4  jccolor.lo  jcsample.o  jdmerge.c   jmemnobs.lo makefile.mms    rdppm.o
ar-lib      jccolor.o   jctrans.c   jdmerge.lo  jmemnobs.o  makefile.sas    rdrle.c
cderror.h   jcdctmgr.c  jctrans.lo  jdmerge.o   jmemsys.h   makefile.unix   rdrle.o
cdjpeg.c    jcdctmgr.lo jctrans.o   jdpostct.c  jmorecfg.h  makefile.vc rdswitch.c
cdjpeg.h    jcdctmgr.o  jdapimin.c  jdpostct.lo jpegint.h   makefile.vms    rdswitch.o
cdjpeg.o    jchuff.c    jdapimin.lo jdpostct.o  jpeglib.h   makefile.wat    rdtarga.c
change.log  jchuff.lo   jdapimin.o  jdsample.c  jpegtran    makejdep.vc6    rdtarga.o
cjpeg       jchuff.o    jdapistd.c  jdsample.lo jpegtran.1  makejdsp.vc6    stamp-h1
cjpeg.1     jcinit.c    jdapistd.lo jdsample.o  jpegtran.c  makejdsw.vc6    structure.txt
cjpeg.c     jcinit.lo   jdapistd.o  jdtrans.c   jpegtran.o  makejfil.v10    testimg.bmp
cjpeg.o     jcinit.o    jdarith.c   jdtrans.lo  jquant1.c   makejmak.vc6    testimg.jpg
ckconfig.c  jcmainct.c  jdarith.lo  jdtrans.o   jquant1.lo  makejsln.v10    testimg.ppm
coderules.txt   jcmainct.lo jdarith.o   jerror.c    jquant1.o   makejvcx.v10    testimgp.jpg
config.guess    jcmainct.o  jdatadst.c  jerror.h    jquant2.c   makeproj.mac    testorig.jpg
config.log  jcmarker.c  jdatadst.lo jerror.lo   jquant2.lo  makerdep.vc6    testprog.jpg
config.status   jcmarker.lo jdatadst.o  jerror.o    jquant2.o   makerdsp.vc6    transupp.c
config.sub  jcmarker.o  jdatasrc.c  jfdctflt.c  jutils.c    makerfil.v10    transupp.h
configure   jcmaster.c  jdatasrc.lo jfdctflt.lo jutils.lo   makermak.vc6    transupp.o
configure.ac    jcmaster.lo jdatasrc.o  jfdctflt.o  jutils.o    makervcx.v10    usage.txt
depcomp     jcmaster.o  jdcoefct.c  jfdctfst.c  jversion.h  maketdep.vc6    wizard.txt
djpeg       jcomapi.c   jdcoefct.lo jfdctfst.lo libjpeg.la  maketdsp.vc6    wrbmp.c
djpeg.1     jcomapi.lo  jdcoefct.o  jfdctfst.o  libjpeg.map maketfil.v10    wrbmp.o
djpeg.c     jcomapi.o   jdcolor.c   jfdctint.c  libjpeg.txt maketmak.vc6    wrgif.c
djpeg.o     jconfig.bcc jdcolor.lo  jfdctint.lo libtool     maketvcx.v10    wrgif.o
example.c   jconfig.cfg jdcolor.o   jfdctint.o  ltmain.sh   makewdep.vc6    wrjpgcom
filelist.txt    jconfig.dj  jdct.h      jidctflt.c  makcjpeg.st makewdsp.vc6    wrjpgcom.1
install-sh  jconfig.h   jddctmgr.c  jidctflt.lo makdjpeg.st makewfil.v10    wrjpgcom.c
install.txt jconfig.mac jddctmgr.lo jidctflt.o  makeadsw.vc6    makewmak.vc6    wrjpgcom.o
jaricom.c   jconfig.manx    jddctmgr.o  jidctfst.c  makeasln.v10    makewvcx.v10    wrppm.c
jaricom.lo  jconfig.mc6 jdhuff.c    jidctfst.lo makecdep.vc6    makljpeg.st wrppm.o
jaricom.o   jconfig.sas jdhuff.lo   jidctfst.o  makecdsp.vc6    maktjpeg.st wrrle.c
jcapimin.c  jconfig.st  jdhuff.o    jidctint.c  makecfil.v10    makvms.opt  wrrle.o
jcapimin.lo jconfig.txt jdinput.c   jidctint.lo makecmak.vc6    missing     wrtarga.c
jcapimin.o  jconfig.vc  jdinput.lo  jidctint.o  makecvcx.v10    rdbmp.c             wrtarga.o
jcapistd.c  jconfig.vms jdinput.o   jinclude.h  makeddep.vc6    rdbmp.o
jcapistd.lo jconfig.wat jdmainct.c  jmemansi.c  makeddsp.vc6    rdcolmap.c
jcapistd.o  jcparam.c   jdmainct.lo jmemdos.c   makedfil.v10    rdcolmap.o
jcarith.c   jcparam.lo  jdmainct.o  jmemdosa.asm    makedmak.vc6    rdgif.c
jcarith.lo  jcparam.o   jdmarker.c  jmemmac.c   makedvcx.v10    rdgif.o
jcarith.o   jcprepct.c  jdmarker.lo jmemmgr.c   makefile.ansi   rdjpgcom

我想在多平台上使用该程序,所以我宁愿不安装libjpeg库。

1 个答案:

答案 0 :(得分:0)

在编译命令中使用libjpeg:

gcc main.c -ljpeg -o main.o

我为C编写了一个教程:http://www.staerk.de/thorsten/C_Programming_Tutorial