Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved How to build a Lib

    General and Desktop
    3
    4
    523
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      Kofr last edited by

      #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.

      jsulm 1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Can you also show your .pro file ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        K 1 Reply Last reply Reply Quote 0
        • K
          Kofr @SGaist last edited by Kofr

          @SGaist

          TEMPLATE = lib
          CONFIG += c++14 exceptions_off rtti_off
          SOURCES += calclib.cpp
          HEADERS += calclib.h
          DEFINES += CALCLIB
          

          the full project
          https://www.dropbox.com/s/d3ttz06dtxrd2i6/DumbCalc.zip?dl=1

          1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @Kofr last edited by jsulm

            @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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 3
            • First post
              Last post