Working with lib files in VS2013 - c++

I created a lib file called StackExample.lib. There are functions and objects described in Stack.hpp and Example.hpp.
I want to have an easy time importing my library so I wrote another file called StackExample.hpp.
This file contains:
#pragma once
#pragma comment(lib, "StackExample.lib")
#include "Stack.hpp"
#include "Example.hpp"
Unfortunately I can no longer compile StackExample.lib because it can not import itself.
Is there a precompiler statement that I can use so that all importing programs load the lib but my lib would ignore that line?

#ifndef __STACK_EXAMPLE_INTERNAL
#pragma comment(lib, "StackExample.lib")
#endif
and then right click on your library project, select Properties => Configuration Properties => C/C++ => Preprocessor
Add __STACK_EXAMPLE_INTERNAL into "Preprocessor Definitions".
This way your library will define this definition and client's (hopefully) not.

Related

Linking a library fails with LINK1181 on VS17

I am trying to use the ADTF streaming library in my project. When I am including the lib, I get the LNK1181 error. The library comes with the headers, the lib files and dll files.
I have added the path inside the C/C++ -> General -> Additional Include Directories.
In addition, I have added the library inside the Linker -> Input -> Additional Dependencies.
Here is also the error screenshot.
Update: I have changed the location of the dll and the libs to my project path and include it again. It does not complain now about the lib itself. Now I am getting an error LNK2001. I believe it is also a linker error.
And here where it all goes wrong!
Update 2: After I see the full log of the build. This appears, I think this means, the linker can't find my lib. Is that right?
The main application code is as this:
#include "pch.h"
#include <iostream>
#include "adtf_streaming.h"
using namespace adtfstreaming;
int main()
{
std::cout << "Hello World!\n";
IADTFFileReader *pFileReader = IADTFFileReader::Create();
}
and the header file which is trying to read/ import my lib is
#ifndef _ADTF_STREAMING_LIBRARY_DLL_
#define _ADTF_STREAMING_LIBRARY_DLL_
#ifdef WIN32
#ifdef STREAMINGLIB_EXPORTS
#pragma message ("Create ADTF Streaming Library ")
// export symbols
#define DOEXPORT __declspec( dllexport )
#else
#pragma message ("Use dynamic ADTF Streaming Library ")
#ifdef _DEBUG
#pragma comment( lib, "adtfstreamingD_290.lib" )
#else
#pragma comment( lib, "adtfstreaming_290.lib" )
#endif
#define DOEXPORT __declspec( dllimport )
#endif
#else
#ifdef STREAMINGLIB_EXPORTS
#define DOEXPORT __attribute__ ((visibility("default")))
#else
#pragma comment( lib, "adtfstreaming_290.lib" )
#define DOEXPORT __declspec( dllimport )
#endif
#endif
//standard includes
#include <stdlib.h>
#include <string.h>
//adtf base types and errors
#include "adtf_base_ref.h"
//streaming lib version
#include "adtf_streaming_version.h"
//adtf streaming lib package headers
#include "adtf_streaming_pkg.h"
#endif //_ADTF_STREAMING_LIBRARY_DLL_
You need to specify the Additional Library Directories, in Linker properties, to set the directory where you have the lib file. You don't need to include the libs in Additional Dependencies because you are doing it in the lib header file #pragma comment( lib, "adtfstreamingD_290.lib" ) when you compile your app in debug or #pragma comment( lib, "adtfstreaming_290.lib" ) when you compile in release. But you need to specify where are these libs in Additional Library Directories.
If you see the lib include file, you see that if STREAMINGLIB_EXPORTS macro is defined all functions with DOEXPORT modifier are exported functions #define DOEXPORT __declspec( dllexport ). But if this macro is not defined #define DOEXPORT __declspec( dllimport ), the same functions are imported functions. It is because the dll needs to specify that this functions are exported functions, so in the dll code someone has defined this macro. Because in your code you have not (and you must not do) define this macro, this functions are imported functions.
ADTF Streaming Library requires VS 2010 and is not compatible with other versions! So make sure to use it with v100 build tools. Or change to ADTF File Library a.k.a. IFHD, which is the v141 compatible successor and works with ADTF 2.x and ADTF 3.x as well. Note that the Lib comes completely open source licensed. See ADTF .dat trace file reader for some overview
I found the answer to the problem. Well, a combination of problems.
The library was built to support 0x86 machines only. I have built it again to support 0x64 and it worked.
P.S. It worked on Visual Studio 2017 too, unfortunately the documentation is poor and lacks information.

Custom Framework using static lib - Include of non-modular header inside framework module Xcode 9

I have seen plenty of answers around about the issue but non of them are based on my case and "ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES" does not work either.
I am developing a framework that is an implementation of a C library. The library is OpenHome and after compiling it and creating the fat libs I have a folder with all the .a and the headers.
Since It has a folder for "Debug" and "Release", I copy this 2 folders in the root of my project, I import the .a files into my "Link binary with libraries" and, in the "Build Settings" of my target, I set the "Header Search Path" with the location of the headers folder.
for importing all the Headers I need to implement I use a c++ class called "MyHeaders.hpp & MyHeader.cpp", I make the the .hpp pubic and I import it like that in my MyFramework.h (Umbrella file):
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#import "MyHeaders.hpp"
#pragma clang diagnostic pop
MyHeaders.hpp:
#ifndef MyHeaders_hpp
#define MyHeaders_hpp
#include <stdio.h>
/*** CP -PROXIES- ***/
/** Header That Includes all the CP Related Headers **/
#include "OpenHome/Net/C/CpStack.h"
/** CP Services **/
/*UPnP*/
#include "OpenHome/Net/C/CpUpnpOrgConnectionManager1.h"
#include "OpenHome/Net/C/CpUpnpOrgRenderingControl1.h"
#include "OpenHome/Net/C/CpUpnpOrgAVTransport1.h"
#endif /* MyHeaders_hpp */
The error come from each include. I have replaced wit import but it also doesn't work.
It is worth mentioning that this project configuration was the one I used in the project (Single View Application) I started implementing and testing. The only difference was the existence of the bridging header.
Any Ideas?
Well after a researching i found the issue and the solution
When I am #include "OpenHome/Net/C/CpUpnpOrgConnectionManager1.h" this header is either not public or contains non public headers.
The solution for importing this headers in a Swift based framework is to to create a Module.
I have created a folder "MyModuleFolder" in my target folder and I have created a "module.modulemap" file inside that looks like
module OHNet[system]{
header "MyHeaders.hpp"
export *
}
After that, in "Build Settings" -> "Swift Compiler - Search Paths " -> "Import Paths" i have added the location for "MyModuleFolder".
Finally,
import OHNet
in each swift file
I hope it helps.

Error including precompiled header

I am writing a code for console application in using Visual studio 2010 on windows 7. I have included the libraries used in this but when I am building the program with header files only it is unable to include "stdafx.h" header file. This is my code.
#include "stdafx.h"
#include "stgetriggersample.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
I am getting this error:
error C1189: #error : "include 'stdafx.h' before including this file for PCH"
"stdafx.h" header file is present in my current directory.
Update
The content of "stgetriggersample.h" is
// StGETriggerSample.h : main header file for the PROJECT_NAME application
//
//#include"stdafx.h"
#pragma once
#ifndef __AFXWIN_H__
#include"stdafx.h"
#error "include 'stdafx.h' before including this file for PCH"
#endif
#include "resource.h" // main symbols
// CStGETriggerSampleApp:
// See StGETriggerSample.cpp for the implementation of this class
//
class CStGETriggerSampleApp : public CWinApp
{
public:
CStGETriggerSampleApp();
// Overrides
public:
virtual BOOL InitInstance();
// Implementation
DECLARE_MESSAGE_MAP()
};
extern CStGETriggerSampleApp theApp;
The content of "stdafx.h" is:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
I tried writing the code with the option Not Using Precompiled Headers but still I get the same error.
How this can be corrected. Thanks
Perhaps you have been trying to fix this so hard, that, in fact, you have broken something. Let's go through proper configuration of pre-compiled headers usage in Visual Studio (I will use options names from VS2012):
Enable usage of PCH: Project Properties -> Configuration Properties -> C/C++ -> Precompiled Headers -> Precompiled Header -> Use (/Yu).
Set name of precompiled header: Project Properties -> Configuration Properties -> C/C++ -> Precompiled Headers -> Precompiled Header File. In your case you should see there standard PCH name (stdafx.h). You may change that if you want.
(In fact, the most important step) Enable PCH generation: Header is not enough. You also need a .cpp file, that is responsible for PCH content generation (since only source files can be compiled). In you case, you have default stdafx.cpp attached to your project. Click it with RMB, then: Configuration Properties -> C/C++ -> Precompiled Headers -> Precompiled Header -> Create (/Yc).
From now on, first non-empty line of every source file attached to your project (including the one from step 3, responsible for PCH generation) should look like this:
#include "PCH_NAME"
Where PCH_NAME is the name of precompiled header set in step 2.
After all these settings are set this way, I would suggest you to:
Clean your solution.
Check in project properties paths of Output Directory and Intermediate Directory. Delete them (I hope they don't contain any code - they shouldn't).
In solution folder (*), you should also see folder with name like: your solution name-cc50b4e6. Delete it.
This will make full reset of your solution (including IntelliSense database etc.). Then, open your project and compile it.
(*) - In your solution folder, by default. If you have valid Fallback Location set, it will be placed there.
In the code:
#ifndef __AFXWIN_H__
#include"stdafx.h"
#error "include 'stdafx.h' before including this file for PCH"
#endif
it looks like StGETriggerSample.h is checking that stdafx.h has been #included by looking for the compile guard macro AFXWIN_H__. However, stdafx.h is not actually defining this guard macro, it's just relying on the statment "#pragma once" to avoid multiple inclusions. So even if stdafx.h has been included, the #error statement will always trigger.
To fix, add the compile guard
#ifndef __STDAFX_H__
#define __STDAFX_H__
// ... yadda yadda yadda...
#endif // __STDAFX_H__
to your stdafx.h. You can remove the #pragma once statement from stdafx.h if you want to, but it's not necessary.
As an aside, your code is also attempting to #include stdafx.h if it hasn't already been included, but then causing the compile error anyways. I'm pretty sure you only want to do one or the other...

How to use functions from different C++ projects in Visual Studio 2010?

I would like to build two C++ projects in the same solution in Visual Studio 2010 that can interact with each other. I have created a solution under directory C:\Users\me\Desktop\SolutionDir. The two projects have been created respectively under C:\Users\me\Desktop\SolutionDir\FirstProject and C:\Users\me\Desktop\SolutionDir\SecondProject.
My first project contains two files, a header function.h and a cpp file function.cpp
function.h
#pragma once
void print_stuff();
function.cpp
#include "function.h"
#include <iostream>
void print_stuff() {
std::cout << "hello world" << std::endl;
}
My second project contains the main file main.cpp
main.cpp
#include "FirstProject\function.h"
#include <iostream>
int main(void) {
print_stuff();
int stop;
std::cin >> stop;
return 0;
}
I added the directory C:\Users\me\Desktop\SolutionDir\ in my SecondProject Configuration Properties > C/C++ > General > Additional Include Directories. I still get the classical error : error LNK2019: unresolved external symbol when calling the function print_stuff().
Any ideas ?
Try building the first project as a Static Library in Project Properties/Configuration Properties/General/Configuration Type.
Then in your project properties for the second project, you'll need to change two things:
In Linker/General, you might need to add to "Additional Library Directories" the folder where the first project's .lib is built.
In Linker/Input, you will need to add to Additional Dependencies the name of the .lib file like FirstProject.lib or whatever its name is.
Yes, you need to export the functions using _declspec(dllexport) and import them in the project that calls the functions with _declspec(dllimport).
This duality is usually achieved with a macro:
#pragma once
#ifdef FIRST_PROJECT_BUILD
#define IMPEXP _declspec(dllexport)
#else
#define IMPEXP _declspec(dllimport)
#endif
IMPEXP void print_stuff();
In the configuration of your first project, you add FIRST_PROJECT_BUILD to your preprocessor directives. That way, when you compile first project, you tell the compiler the function is to be exported. However, when you include the file in a different project, that doesn't have FIRST_PROJECT_BUILD defined, you tell the compiler the function is implemented in a different library and should be imported.
Also, besides adding the extra include paths, you need to add the generated .lib files from the projects implementing the functions to the Extra dependencies tab in the Liner settings of your project configuration.
You can include a realPath for you include directory !
Like for your FirstProject include "./../"
And the same include dir for your second Project like that you can move or copy your directory SolutionDir and it will always work !
For your unresolved linked you have to add the function.cpp and function.h in your First and Second Project
You can place it in SolutionDir!
Like that You always have two files for your first and second project instead of four !
Hope it helps !

Using preprocessor directives to define command line options

If I wanted to add, let's say, a new .lib to the build only if a particular #define was set, how would I do that?
In the MSVC++ 2008 "Property Pages", you would simply add: Config Properties -> Linker -> Input -> Additional Dependencies, but I would like it if something like #define COMPILE_WITH_DETOURS was set, then the particular library would be added to the dependencies, otherwise it would be removed.
You can set some linker options by using #pragma comment in one of your source files.
For example, to link against a 'detours.lib' library only if COMPILE_WITH_DETOURS is defined, you can use:
#ifdef COMPILE_WITH_DETOURS
# pragma comment(lib, "detours.lib")
#endif
(this is specific to Microsoft Visual C++ and is not portable)