7

I am using Microsoft Visual Studio 2010, and i am working on open source Clamav, my code is given below which is generating an error

#include <stdio.h>
#include <stdlib.h>  
#include <string.h>
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <clamav.h>


int main(int argc, char **argv)
{
int fd, ret;
unsigned long int size = 0;
unsigned int sigs = 0;
long double mb;
const char *virname;
struct cl_engine *engine;


if(argc != 2) {
printf("Usage: %s file\n", argv[0]);
return 2;
}

if((fd = open(argv[1], O_RDONLY)) == -1) {
printf("Can't open file %s\n", argv[1]);
return 2;
}

if((ret = cl_init(CL_INIT_DEFAULT)) != CL_SUCCESS) {
printf("Can't initialize libclamav: %s\n", cl_strerror(ret));
return 2;
}

if(!(engine = cl_engine_new())) {
printf("Can't create new engine\n");
return 2;
}

/* load all available databases from default directory */
if((ret = cl_load(cl_retdbdir(), engine, &sigs, CL_DB_STDOPT)) != CL_SUCCESS) {
printf("cl_load: %s\n", cl_strerror(ret));
close(fd);
    cl_engine_free(engine);
return 2;
}

printf("Loaded %u signatures.\n", sigs);

/* build engine */
if((ret = cl_engine_compile(engine)) != CL_SUCCESS) {
printf("Database initialization error: %s\n", cl_strerror(ret));;
    cl_engine_free(engine);
close(fd);
return 2;
}

/* scan file descriptor */
if((ret = cl_scandesc(fd, &virname, &size, engine, CL_SCAN_STDOPT)) == CL_VIRUS) {
printf("Virus detected: %s\n", virname);
} else {
if(ret == CL_CLEAN) {
    printf("No virus detected.\n");
} else {
    printf("Error: %s\n", cl_strerror(ret));
    cl_engine_free(engine);
    close(fd);
    return 2;
}
}
close(fd);

/* free memory */
cl_engine_free(engine);

/* calculate size of scanned data */
mb = size * (CL_COUNT_PRECISION / 1024) / 1024.0;
printf("Data scanned: %2.2Lf MB\n", mb);

return ret == CL_VIRUS ? 1 : 0;
}

the following error is generated LINK : fatal error LNK1181: cannot open input file 'libclamav.lib'

kindly guide me

WiXXeY
  • 981
  • 5
  • 19
  • 46
  • Conventionally what's libXYZ.a in UNIX-world is XYZ.lib in Windows-world. You appear to be mixing the two worlds. – trojanfoe Aug 21 '13 at 08:11
  • There is probably nothing wrong with your code. The error is in the way you compile it. Please give some detail. – hivert Aug 21 '13 at 08:13
  • 3
    Added library directories to project properties (Configuration Properties->Linker->General->Additional Library Directories)? – user1837009 Aug 21 '13 at 08:15

2 Answers2

16

You get an LNK1181 error in Visual Studio when the .lib or .obj files that are specified during linking are not found in the current directory, any of the directories that are specified by the LIBPATH linker option, or any of the directories that are specified in the LIB environment variable.

You may add the directory that contains libclamav.lib library file to the LIBPATH to resolve the problem (this instructions may vary a bit depending on your Visual Studio version):

  1. In Solution Explorer, right-click the project, and then click Properties.
  2. In the Property Pages dialog box, expand Linker, and then click General.
  3. In the Additional Library Directories field, specify the path where libclamav.lib resides.

The error can also happen when the LIBPATH contains spaces. If that's the case, move the library to a path without spaces or put quotation marks around the path.

Daniel Martín
  • 7,815
  • 1
  • 29
  • 34
  • 1
    I wish I could give you 1000 upvotes! I am trying to update a legacy project, and the LIBPATH had spaces in it. I've been searching for what seems like hours, and no one else suggested that. I have no idea how it was working on the original dev environment! Maybe they also happened to have it in the LIB envirable... In any event, I don't know how moderner versions of VS deal with that, but VC++ 6 (Don't ask!) did not fail gracefully whatsoever. – shiser May 19 '14 at 09:12
  • Just in case anyone else stumbles upon this, my mistake was more basic and silly. I was attempting to build within the "project only". Make sure to build your dependencies first! So right click on your project file and click build. – Karoh Dec 22 '15 at 20:47
1

You can also fix it by specifying the library path in DOS "8.3" format.

To get the 8.3 form, do (at the command line):

DIR /AD /X

recursively through every level of the directories.

Pierre
  • 4,114
  • 2
  • 34
  • 39