I have been playing around with wlanapi in windows. I did not have any problem compiling or running until I tried using the function WlanScan. Then I was unable to compile due to "WlanScan" not being declared in the scope. I wrote a really short program illustrating this using two functions: WlanOpenHandle that works and WlanScan which doesn't.
#include <windows.h>
#include <wlanapi.h>
int main()
{
HANDLE hClient;
WlanOpenHandle(2, 0, 0, &hClient);
WlanScan(hClient, 0, 0, 0, 0);
}
Compiling that single file like this:
g++ main.cpp -lwlanapi
Results in this error:
main.cpp: In function 'int main()':
main.cpp:9:30: error: 'WlanScan' was not declared in this scope
WlanScan(hClient, 0, 0, 0, 0);
^
What could be the cause of this? I've been able to use a handful of functions from the wlanapi. I am on Windows 7 compiling with minGW.
EDIT:
In accordance to what u/ IInspectable said, I changed the command used to compile to:
g++ -D_WIN32_WINNT=_WIN32_WINNT_WIN7 main.cpp -lwlanapi
And it worked!
It looks like someone else has had this problem before:
How To Compile C++ Code that has a 'wlanapi.h' and 'windows.h' dependency
The recommended solution is to just put it into Visual Studio and compile it using that; MinGW probably isn't able to find the library.
Using VS2010 I created a VC++ console application (with a precompiled header), and I was able to get the following to compile without any errors:
// wlanapi_Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hClient;
WlanOpenHandle(2, 0, 0, &hClient);
WlanScan(hClient, 0, 0, 0, 0);
}
And here's my precompiled header:
// 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
#include <windows.h>
#include <wlanapi.h>
#pragma comment(lib, "wlanapi.lib")
Related
While following the book C++ For Dummies, I have three files in my CodeBlocks project, main.cpp, Pen.h, and Pen.cpp. They look like this:
main.cpp:
#include <iostream>
#include "Pen.h"
//#include "Pen.cpp"
using namespace std;
int main()
{
Pen MyPen = Pen();
MyPen.test();
}
Pen.h:
#ifndef PEN_H_INCLUDED
#define PEN_H_INCLUDED
//#include "Pen.cpp" // Uncommenting this gives a different error
using namespace std;
class Pen
{
public:
// attributes omitted
// PROTOTYPES:
// other functions omitted
void test();
};
#endif // PEN_H_INCLUDED
Pen.cpp:
#include <iostream>
#include "Pen.h"
using namespace std;
//other function definitions omitted
void Pen::test()
{
cout << "Test successful." << endl;
}
When I run the code as listed above, I get an "undefined reference to `Pen::test()'" error. To fix this, I changed the #include statements at the top of main.cpp to:
#include <iostream>
//#include "Pen.h"
#include "Pen.cpp"
This works as intended and correctly prints out "Test successful."
My question is this: what in the world is the point of putting a function prototype in a header file if I have to import the .cpp file later on anyways?
EDIT: It turns out this was a problem with not knowing how to use Code::Blocks rather than with the C++ language.
Assuming you're using gcc, you can compile and link in one step by supplying multiple .cpp files via the command line.
g++ Pen.cpp main.cpp
clang should be similar.
clang++ Pen.cpp main.cpp
An #include should never reference a .cpp file. At all. There's no good reason to do it. Include your headers and then supply the names of all .cpp files when you compile. If your project gets big and you have too many .cpp files to reasonably list, then it's time to break out a makefile or similar.
In the main.cpp include the header file:
#include "Pen.h"
The Pen.h file it's ok.
You need to add the Pen.cpp file to the project tree.
Go to Project -> Add files... and add Pen.cpp
I am trying to get all the system paths of windows. When reading this it says to use KNOWNFOLDERID. So I followed the example from here. When try to use that example I get a compilation error.
Test.cpp:
#include <tchar.h>
#include <sysinfoapi.h>
#include <Shlobj.h>
#include <combaseapi.h>
#include <WTypesbase.h>
#include <winnt.h>
int main() {
IKnownFolderManager *pManager;
HRESULT hr = CoCreateInstance(
CLSID_KnownFolderManager,
NULL, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pManager)
);
}
This is what I have so far. The error I get is
error:
invalid static_cast from type 'IKnownFolderManager*' to type
'IUnknown*'
static_cast<IUnknown *> (*pp);
I am compiling from command line in windows 10 as: g++ test.cpp
IKnownFolderManager requires <Shobjidl.h>. Without that include, the compiler doesn't know how to cast IKnownFolderManager to IUnknown.
I was looking for a native API access in GLFW3 documentation to get HWND but it's not in my GLFW/glfw3.h file. Is there any #define's to be able to find it by compiler? I can't also find it manually in the file itself using text-finder, so how can I get it?
PS. I can't tag glfw3.
Edit:
Code:
#define GLFW_INCLUDE_GLU
#define GLFW_EXPOSE_NATIVE_WGL
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GL/GLFW/glfw3native.h>
#include <GL/GLFW/glfw3.h>
#include <ctime>
#include <cstdlib>
...
int main()
{
//glfw setup
...
//bla bla bla
...
//all I want to do is to call this one
ScreenToClient( glfwGetWin32Window(window), &point);
}
After getting confused by your problems I tried it my self and I think the include order is your problem. A minimal code example that mimics on Linux what you try to do on Windows compiles and works as intended:
#define GLFW_EXPOSE_NATIVE_X11
#define GLFW_EXPOSE_NATIVE_GLX
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
int main (int argc, char ** argv)
{
glfwInit();
GLFWwindow* window = glfwCreateWindow (256, 256, "GLFW", nullptr, nullptr);
glfwGetX11Window(window);
glfwTerminate();
return 0;
}
EDIT: Added the incovation of glfwTerminate() for proper clean-up. Please note, of course there should be appropriate error checking taking place, but for the purpose of demonstrating a minimal example, the above is sufficient.
I recently installed MinGW and MSYS on my Windows 32 machine and it seems to be running fine.
On the C++ compiler, I am including a vector container and getting no errors to that. But I`m getting compile-time errors when I try to use it.
So, the code
#include <vector> // include vector.h
#include <stdio.h> // include stdio.h
using namespace std;
main() {
// vector<int> A;
printf("\nHeya ..");
}
is running just fine. However, the moment I un-comment line 8-- the vector declaration line, I get the following error (shortened) in compile time:
undefined reference to 'operator delete(void*)'
undefined reference to '__gxx_personality_v0'
You're probably compiling with gcc instead of g++. The actual compiler is the same, but g++ tells the linker to use the default C++ libraries, were gcc only tells it to look at the C libraries. As soon as you use and C++-specific parts of the standard library, gcc will fail.
As an aside, C++ doesn't support the default int rule from old C, so you should really specify the return type from main.
I don't see how you are compiling your code. Your main method is invalid, incorrect signature and you aren't returning anything.
Should be like this:
#include <vector> // include vector.h
#include <stdio.h> // include stdio.h
using namespace std;
int main(int, char**) {
// vector<int> A;
printf("\nHeya ..");
return 0;
}
Also you need to compile this with g++ and not gcc.
I am trying to implement a test project using the Point Cloud Library and OpenCV with multiple files. When I try to compile, I get the "already defined error" message. Probably I'm doing something stupid that cannot realize for some reason - I tried out a couple of solutions found here, none of them seemed to be help in my case.
What I have:
A libs.h file, where I load the lib files (in Project properties, I only set up the .lib paths and load the libs "by hand", like the headers):
#pragma once
#ifndef PCLTEST_LIBS
#define PCLTEST_LIBS
#ifdef _DEBUG
#pragma comment(lib, "pcl_apps-gd.lib")
#pragma comment(lib, "pcl_common-gd.lib")
// a bunch of other debug libs
#else
// the release libs
#endif
#endif
A main file from which I basically deleted everything at this point to debug:
// load the libs
#ifndef PCLTEST_LIBS
#include "libs.h"
#endif
// pcltest includes
// if only this first one is #included, everything is OK
#include "opencvOperations.h"
// #including this one causes the error
#include "files.h"
// these ones are not working also
//#include "cloudOperations.h"
//#include "visualize.h"
// c++ headers
#include <stdio.h>
#include <string>
//#include <sstream>
//#include <iostream>
void writeInfo()
{
// some std::cout calls
}
int main( int argc, char* argv[] )
{
writeInfo();
// this function is in opencvOperations.h and works OK
pcltest::openLena();
}
Then I get several error messages in my main.obj that some (PCL related) symbols are already defined in files.obj. I use PCL related calls both in opencvOperations and files, the first one is OK, the second one does not work.
Edit:
To add more detail, my files.h header:
#pragma once
#ifndef PCLTEST_FILES
#define PCLTEST_FILES
// pcl headers
#ifndef PCL_COMMON_H_
#include <pcl/common/common_headers.h>
#endif
#ifndef PCL_IO_FILE_IO_H_
#include <pcl/io/file_io.h>
#endif
#ifndef PCL_IO_PCD_IO_H_
#include <pcl/io/pcd_io.h>
#endif
#ifndef PCL_IO_PLY_IO_H_
#include <pcl/io/ply_io.h>
#endif
// boost headers
#ifndef BOOST_FILESYSTEM_OPERATIONSX_HPP
#include <boost/filesystem/operations.hpp>
#endif
#endif
namespace pcltest
{
// function to open PCL or binary PLY files
pcl::PointCloud<pcl::PointXYZ>::Ptr openCloud(std::string filename);
// function to save the point cloud to PCD format
void saveCloud();
}
Before splitting the code into separate files, everything worked well (with the same project settings).
Edit2:
I located the source of the problem,
#include <pcl/io/ply_io.h>
causes this. For now, I got rid of everything related to PLY and everything works fine. I'll look at it later, this might be a PCL library specific issue. Still strange to me why this call causes linker error in an other file, where I don't even use PLY related functions/variables.
I had the same problem as you had. I had a surface.h and surface.cpp file, and I found out that I had to include the ply_io.h file from surface.cpp rather than surface.h and now it compiles fine. I hope that helps or makes sense! haha
If a constant is being instantiated in an include one can also use selectany, per http://msdn.microsoft.com/en-us/library/5tkz6s71%28v=vs.80%29.aspx -- in my case:
const int CSdata[] = {1, 2, 3, 4};
when included in more than one source part produces LNK2005 at link time, avoided via:
const __declspec(selectany) int CSdata[] = {1, 2, 3, 4};
Non-portable, yeah ...