locked
C/C++: LINKING LIB AND OBJ FILES RRS feed

  • Question

  • I'm using open source C code which requires building libraries with NMAKE.

    I create an empty VS Studio 10 project and copy in the open source then I NMAKE each library.

    Then I add my code which uses functions in those libraries and add these explicitly to the VS 10 solution explorer (Header and Source folders).

    My developed code has a "main" cpp driver and a library class (a cpp with header file)

    I can successfully build ALL the STATIC .libs for the open source code with NMAK -- no problems here.

    I'd rather not add the cpp main and the cpp library file I created to any of the static libraries I compiled with NMAKE -- I'd like the original libraries separate. Meaning NOT include this in the Makefile.vc file.

    I have done the following:

    For the open src:

    Pointed to all the .h directories (C/C++ Properties > Additional Dependencies)

    Pointed to the all the lib paths (Linker > Additional Dependencies)

    Explicitly specified the lib (static lib files) by name (Linker > Inputs)

    For the my code:

    Added my class header file path to the list above. (C/C++ > Additional Deps)

    Added the path where my obj files to the static Lib paths above. (Linker > Additional Deps)

    Explicitly named the two obj files to the list of the named library files. (Linker > Inputs > Additional Deps)

    I also attempted to create a static library project and attempt to link that way: I found that when I used the Resources dialog and attempted to add a resource, non were found to add.

    Any advice or suggestions would be appreciated.


    • Edited by dmk4170 Friday, February 26, 2016 6:58 PM
    Friday, February 26, 2016 5:34 PM

Answers

  • I see function name mangling which leads me to believe I need some Extern "C" specifications in my library and main.

    Most likely so.

    I need to figure where I need the EXTERN call in my code.

    Usually, the #include statement of the C library is enclosed in extern "C" { ... }

    What I neglected to mention is that the open source (libgeotiff/libtiff/proj4) are all "C" code.  My 3 module are cpp.  So I believe this one problem.


    • Proposed as answer by Hart Wang Thursday, March 3, 2016 9:07 AM
    • Marked as answer by dmk4170 Friday, May 20, 2016 5:39 PM
    Tuesday, March 1, 2016 2:24 PM

All replies

  • Do you receive some error message? If so, please post it.

    What you do, looks OK, just make sure that compilation options match for the main  app and the library. Especially runtime library variant (DLL or static, debug or release) and  32 vs 64 bit.

    -- pa

    Sunday, February 28, 2016 12:16 AM
  • Pavel

    Truly appreciate your effort to assist!.

    Thank you for your PROMPT response -- I'm getting LNK2019 (unresolved external symbols) errors and I see function name mangling which leads me to believe I need some Extern "C" specifications in my library and main.

    My class library contains functions which call in the STATIC libs. These are unresolved.  So the compiler is unable to link my code with functions in the static lib (geotiff.lib).   I need to figure  where I need the EXTERN call in my code.  

    What I neglected to mention is that the open source (libgeotiff/libtiff/proj4) are all "C" code.  My 3 module are cpp.  So I believe this one problem.

    Tuesday, March 1, 2016 12:34 PM
  • I see function name mangling which leads me to believe I need some Extern "C" specifications in my library and main.

    Most likely so.

    I need to figure where I need the EXTERN call in my code.

    Usually, the #include statement of the C library is enclosed in extern "C" { ... }

    What I neglected to mention is that the open source (libgeotiff/libtiff/proj4) are all "C" code.  My 3 module are cpp.  So I believe this one problem.


    • Proposed as answer by Hart Wang Thursday, March 3, 2016 9:07 AM
    • Marked as answer by dmk4170 Friday, May 20, 2016 5:39 PM
    Tuesday, March 1, 2016 2:24 PM
  • So I changed my plan...

    I created a VS2010 Empty Project I then copied all there open sources "C" baselines into the library.

    For GEOTIFF it three open source baselines (Proj4 (4.8.0), tiff (4.0.3), and libgeotiff (1.4.0) - I'll call this the "repository."

    I added all the *.c and *.h files for each baseline in the Source and Header filters in the STATIC LIB proj

    I also created sub-filters under each (Proj, Tiff, Geotiff) -- to better organize the chaos.

    Then linked all in to a static library. This took quite some time, fighting with so many files and removing optional code. But I got it.  Naturally I also set up the project options; pointing to all the include file folders for each open source baseline.

    Then I created a NEW project and was able employ static library functions -- which  I created in my STATIC LIBRARY project.  All good.  I ran some basic tests and was able to access lib functions. I have not seen anywhere else on the web of this strategy employed for LIBGEOTIFF.  Most run three nmakes. I was surprised that in my second project I STILL need to point to the "repository" *.h files as they are  required. In addition I naturally specified my STATIC library and its path in the LINKER settings.


    • Edited by dmk4170 Thursday, March 3, 2016 2:12 PM
    Thursday, March 3, 2016 1:51 PM
  • This is how "open source" works: you get the source free and pay with your time and effort later...

    Thursday, March 3, 2016 4:17 PM