Thursday, January 21, 2021

Static Library How to create and use it in client application? / how to add C++ source code file into project?

How to add C++ source code file into project?

    Microsoft Authoritive Information

  1. VC++ Directories Property Page (Windows)
  2. Use this property page to tell Visual Studio which directories to use when building the currently-selected project. To set directories for multiple projects in a solution, use a custom property sheet as described in Share or reuse Visual Studio C++ project settings.

    Reference Directories Directories in which to search for assembly and module (metadata) files that are referenced in the source code by the #using directive. Corresponds to the LIBPATH environment variable.

    Library Directories Directories in which to search for libraries (.lib) files; this includes run-time libraries. Corresponds to the LIB environment variable. This setting does not apply to .obj files; to link to an .obj file, on the Configuration Properties > Linker > General property page, select Additional Library Dependencies and then specify the relative path of the file. For more information, see Linker property pages.

  3. /LIBPATH (Additional Libpath)
  4. /LIBPATH:dir

    Parameters

    dir

    Specifies a path that the linker will search before it searches the path specified in the LIB environment option.

    Remarks

    Use the /LIBPATH option to override the environment library path. The linker will first search in the path specified by this option, and then search in the path specified in the LIB environment variable. You can specify only one directory for each /LIBPATH option you enter. If you want to specify more than one directory, you must specify multiple /LIBPATH options. The linker will then search the specified directories in order.

    To set this linker option in the Visual Studio development environment

    1. Open the project's Property Pages dialog box. For details, see Set C/C++ compiler and build properties in Visual Studio. expand the Configuration Properties node--> expand Linker node --> and then select General node -->Additional Library Directores: click the dropdown arrow -> click Edit -> type relative path to .lib file location, type such as: ..\StaticLib1\Debug\
    2. Click the Linker folder.
    3. Click the General property page.
    4. Modify the Additional Library Directories property.
  5. VC++ Directories Property Page (Windows)
  6. Include and libraries paths
  7. How do you set up the include and libraries paths in VS 2008 C++?.

    Details

  8. Walkthrough: Compiling a Native C++ Program on the Command Line
  9. a simple demo is:

    To compile a program that has additional source code files, enter them all on the command line, like:

    cl.exe /EHsc file1.cpp file2.cpp file3.cpp

    The /EHsc command-line option instructs the compiler to enable standard C++ exception handling behavior. Without it, thrown exceptions can result in undestroyed objects and resource leaks. For more information, see /EH (Exception Handling Model).

    When you supply additional source files, the compiler uses the first input file to create the program name. In this case, it outputs a program called file1.exe. To change the name to program1.exe, add an /out linker option:

    cl.exe /EHsc file1.cpp file2.cpp file3.cpp /link /out:program1.exe

    And to catch more programming mistakes automatically, we recommend you compile by using either the /W3 or /W4 warning level option:

    cl.exe /W4 /EHsc file1.cpp file2.cpp file3.cpp /link /out:program1.exe

    Open a developer command prompt. we need to do some checking:

    1. If you have installed Visual Studio 2017 or later on Windows 10, open the Start menu and choose All apps. Scroll down and open the Visual Studio folder (not the Visual Studio application). Choose Developer Command Prompt for VS to open the command prompt window.

      If you have installed Microsoft Visual C++ Build Tools 2015 on Windows 10, open the Start menu and choose All apps. Scroll down and open the Visual C++ Build Tools folder. Choose Visual C++ 2015 x86 Native Tools Command Prompt to open the command prompt window.

      You can also use the Windows search function to search for "developer command prompt" and choose one that matches your installed version of Visual Studio. Use the shortcut to open the command prompt window.

    2. Next, verify that the Visual C++ developer command prompt is set up correctly. In the command prompt window, enter cl and verify that the output looks something like this:

  10. Walkthrough: Compile a C program on the command line
  11. Walkthrough: Compile a C program on the command line

  12. Walkthrough: Creating and Using a Static Library (C++)
  13. This step-by-step walkthrough shows how to create a static library (a .lib file) for use with C++ apps. Using a static library is a great way to reuse code. Rather than re-implementing the same routines in every app that requires the functionality, you write them one time in a static library and then reference it from the apps. Code linked from a static library becomes part of your app—you don’t have to install another file to use the code.

    need to test it today...

    To create a static library project: (please note: this is old style before VS 2008, now from VS 2017 it is simpler)

    1. Start Visual Studio 2017, On the menu bar, choose File, New, Project.
    2. In the left pane of the New Project dialog box, expand Installed, Templates, Visual C++, and then select Windows Desktop.
    3. In the center pane, select Static Library.
    4. Specify a name for the project—for example, MathFuncsLib—in the Name box. Specify a name for the solution—for example, StaticLibrary—in the Solution Name box. Choose the OK button.
    5. then browse project files and check project properties.
    6. From Configuration Properties-> General -> General Sections: check these attributes:
      1. Target Platform: Windows 10
      2. Windows SDK version: 10.0.188362.0
      3. Output Directory: $(SolutionDir)$(Configuration)\
      4. Immediate Directory: $(Configuration)\
      5. TargetName: $(ProjectName)
      6. TargetExtension: .lib
      From Configuration Properties-> General -> ProjectDefaults Sections: check these attributes:
      1. Configuration Type: static library(.lib)
      2. Use of MFC: use standard windows libraries
      3. Character set: use unicode character set
    7. From Configuration Properties-> Librarian -> General Sections: check these attributes:
      1. Output file:$(OutDir)$(TargetName)$(TargetExt)
    8. From Configuration Properties-> C/C++ -> Precomipled Headers Sections, check these attributes:
      1. Precompiled Headers: Use(/Yu)
      2. Precompiled Header File:pch.h
      3. Precompiled Header Output File:$(IntDir)$(TargetName).pch
    9. Choose the Finish button to create the project.

    To create a C++ console app that references the static library

    1. On the menu bar, choose File, New, Project.
    2. In the left pane, under Visual C++, select Win32.
    3. In the center pane, select Win32 Console Application.
    4. Specify a name for the project—for example, MyExecRefsLib—in the Name box. In the drop-down list next to Solution, select Add to Solution. This adds the new project to the solution that contains the static library. Choose the OK button.
    5. On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.
    6. On the Application Settings page, under Application type, select Console application.
    7. On the Application Settings page, under Additional options, clear the Precompiled header check box.
    8. Choose the Finish button to create the project.

    To use the functionality from the static library in the app

    1. (skip if not in the same solution) After you create a console app, an empty program is created for you. The name for the source file is the same as the name that you chose earlier. In this example, it's named MyExecRefsLib.cpp.
    2. (skip if not in the same solution) Before you can use the math routines in the static library, you must reference it. To do this, open the shortcut menu for the MyExecRefsLib project in Solution Explorer, and then choose References. In the MyExecRefsLibProperty Pages dialog box, expand the Common Properties node, select Framework and References, and then choose the Add New Reference button. For more information about the References dialog box, see Framework and References, Common Properties, <Projectname > Property Pages Dialog Box.
    3. (skip if not in the same solution)The Add Reference dialog box lists the libraries that you can reference. The Projects tab lists the projects in the current solution and any libraries that they contain. On the Projects tab, select the MathFuncsLib check box, and then choose the OK button.
    4. To reference the MathFuncsLib.h header file, you must modify the included directories path. In the Property Pages dialog box for MyExecRefsLib,

      expand the Configuration Properties node--> expand the C/C++ node --> and then select General-->Next to Additional Include Directories: specify the path of the MathFuncsLib directory or browse for it. please note: I use relative path to MathfuncLib.h files such as : ../StaticLib1/.

    5. We also need to tell new client project which lib file to link the .lib file.

      expand the Configuration Properties node--> expand the Linker node --> and then select Input node -->Next to Additional Dependencies: click the dropdown arrow -> click Edit -> type lib name such as: StaticLib1.lib

    6. We also need to tell new client project where to link this .lib file. there are two options to do so.

      first option(Good): copy the StaticLib1.lib directly to project folder(client app) where .cxproj file resides.

      second option(Better): expand the Configuration Properties node--> expand Linker node --> and then select General node -->Additional Library Directores: click the dropdown arrow -> click Edit -> type relative path to .lib file location, type such as: ../StaticLib1/Debug/

      third option(last option): expand the Configuration Properties node--> expand VC++ Directores node --> and then select Input node -->Library Directores: click the dropdown arrow -> click Edit -> type relative path to .lib file location, type such as: ../StaticLib1/Debug/

    7. You can now use the MyMathFuncs class in this app. To do this, replace the contents of MyExecRefsLib.cpp with this code:

  14. How to add additional libraries to Visual Studio project?
  15. Allergro is an open souce C++ addon library for graphics manipulation. How do I add this library to my compiler? The instructions don't work for me as I have Windows 7. I don't know if the OS matters. I have the Visual Studio Express Edition. The library is a .dll file. How do I add it to my projects?

  16. A.2 — Using libraries with Visual Studio
  17. This is a paragraph.

  18. How to include libraries in Visual Studio 2012?
  19. This is a paragraph.

  20. How to Add an External C++ Library to Your Project
  21. This is a paragraph.

  22. Configuring Visual Studio for C/C++ Projects
  23. this gives me another option.

  24. 2.6 — Forward declarations and definitions
  25. 2.7 — Programs with multiple code files
  26. This is a paragraph.

  27. This is a paragraph.

  28. This is a paragraph.

No comments:

Post a Comment