How to build a Lib
-
#ifndef CALCLIB_H #define CALCLIB_H #if defined(CALCLIB) #define CALCLIB_COMMON_DLLSPEC Q_DECL_EXPORT #else #define CALCLIB_COMMON_DLLSPEC Q_DECL_IMPORT #endif namespace CalcLib { enum TypeWork : int { Addition, Subtraction, Division, Multiplication }; enum ErrorCode : int { Div0 = -9, WrongTypeWork = -8, Ok = 0, OperandIsWorng }; } CALCLIB_COMMON_DLLSPEC double DoIt(int TypeWork, double OperandA, double OperandB, int& ErrorCode); #endif // CALCLIB_H
#include "calclib.h" #include <limits> double DoIt(int TypeWork, double OperandA, double OperandB, int &ErrorCode) { switch (TypeWork) { case CalcLib::Addition: ErrorCode = CalcLib::Ok; return OperandA + OperandB; case CalcLib::Subtraction: ErrorCode = CalcLib::Ok; return OperandA - OperandB; case CalcLib::Division: if(OperandB == 0) { ErrorCode = CalcLib::Div0; return std::numeric_limits<double>::infinity(); } ErrorCode = CalcLib::Ok; return OperandA / OperandB; case CalcLib::Multiplication: ErrorCode = CalcLib::Ok; return OperandA * OperandB; default: ErrorCode = CalcLib::WrongTypeWork; return 0; } }
When building I get errors in this line
CALCLIB_COMMON_DLLSPEC double DoIt(int TypeWork, double OperandA, double OperandB, int& ErrorCode);
:-1: error: LNK2001: unresolved external symbol _WinMainCRTStartup
debug\src.exe:-1: error: LNK1120: 1 unresolved externals
D:\dev\testing\DumbCalc\calcLib\calclib.h:24: error: C2144: syntax error: 'double' should be preceded by ';'
D:\dev\testing\DumbCalc\calcLib\calclib.h:24: error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Compiler is MSVC2015 x86
What do I do wrong? How to make it work. -
Hi,
Can you also show your .pro file ?
-
@Kofr said in How to build a Lib:
CALCLIB_COMMON_DLLSPEC double DoIt(int TypeWork, double OperandA, double OperandB, int& ErrorCode);
Why do you name the first parameter same as your first enum (TypeWork) and same for ErrorCode?!
Don't you mean:CALCLIB_COMMON_DLLSPEC double DoIt(CalcLib::TypeWork typeWork, double OperandA, double OperandB, CalcLib::ErrorCode &errorCode);
I can build your project after adding
#include <QtCore/QtGlobal>
in calclib.h