How to use another project namespace? - c++

I'm trying to include a namespace from another project in my project but I get the "symbol could not be resolved" error.
using namespace project;
^ This line gives the "symbol could not be resolved" error.
I have the files with this namespace included in my project.
I'm using eclipse on ubuntu Mate.

The C++ compiler does not know about the project management of your IDE (development environment).
It only sees the source code itself. If you add an #include statement the compiler will also see the included code as if it has been put at that place. #include is recursive, so if one included file includes another that will be seen as well.
The error message means that at the point of your using namespace project the symbol project is not known. So, you obviously miss an earlier #include statement including any header of the other project which contains a namespace project { ... } defining the namespace.
Any implementation (.cpp) file using namespace project needs such an #include statement.

To use a namespace of another project, u have to refer that project in your existing project.
In eclipse, you can add the project by following below steps.
Right click > Properties > C/C++ General > Paths and Symbols
Go to Tab Library and Add new library path
You can see the referenced path, check the project and Apply.
You can now use the namespace.
This Link can give more info about references and other settings of the project in eclipse.

Related

How to use static library that contains C++20 modules

I have a static library project UserInterface that contains the following module:
// Foo.ixx
export module Foo;
export void MyFunc();
void MyFunc()
{
[]() {};
}
The module Foo is imported in the main header of the library like this:
// NuiLibrary.hpp
#pragma once
import Foo;
// Windows
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
// other includes
I cannot build the solution, I can build the static library but in my Sculpt project that references the library I get these errors:
After much search I think it's because modules need to be included explicitly in the client solution by setting Additional Module Dependencies.
What's the syntax for it ?
Why doesn't it just work despite setting this:
What was the cause ?
After a day of trying my specific problem was that the Microsoft Package Server was file locking the .ifc file causing the could not find module error.
How to detect ?
You can find out if you have this issue by trying to compile the module just by itself via right click module -> Compile. It will then fail to compile the file with the error could not open the file another process is using it.
The (non)solution
In a fit of rage I just set everything to default in my static library project setting and now it just works.
What can definitely cause this
When you configure your solution you must do the following:
in the client project add a reference to the static library project (this will remove the error from the client project)
in the static library go Project Settings->C/C++->General and tick the Scan sources for module dependencies (this will remove the error from the library project)
Other Issues
In the older versions of Visual Studio 2019 when you renamed a file from .hpp to .ixx the compiler would still recognize the file as a header file.
To fix this go to the file properties and set the Item type to C/C++ compiler instead of C/C++ header.
In the current version of Visual Studio 2019 when you create a module file you need to go to file properties C/C++->Precompiled headers->Precompiled Header and click Not using precompiled header.
This will fix the following nasty error:
Without having any experience with modules in Visual Studio, I can still see some strange stuff.
Header files should not be used together with modules as we see with your NuLibrary.hpp file. While the syntax looks okay you get problems if that header is included inside the global module fragment (GMF) of other source files that declare modules because import declarations may not appear inside GMF:
// some source file including your header
module;
#include "NuLibrary.hpp" // --> ERROR! import declaration..
What I would recommend is to completely forget the idea of having a "main header" of a module project and instead place all that functionality in a module interface file for your project:
// NuLibrary.cpp
module;
// Windows
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
// other includes
export module nulibrary;
import Foo;
[...]
I suppose Visual Studio would prefer if you have that file the .ixx-extension..

Linking a static library to a new project in Visual C++

I am just beginning to learn C++ and using Visual Studio 2017 as the IDE. I want to stay away from the CLR, and even Windows specific code as much as possible. Every new project option seems to say 'Windows', and thus suggest Windows OS dependencies, except for General > Empty Project, so that is the option I have chosen.
I am trying to write a static library with some functions. Here's a simple example of what I'm trying to do.
//Lib1.h (Project A)
#pragma once
#ifndef LIB_H
namespace Lib1
{
double Add(double, double);
}
//Lib1.cpp (Project A)
#include "Lib1.h"
namespace Lib1
{
double Add(double a, double b)
{
return a + b;
}
}
//Main.cpp (Project B)
#include "Lib1.h"
using namespace Lib1;
int main()
{
double c = Add(1, 2);
}
Project A compiles without warning/error. I changed the Project Properties > General > Configuration Type to be Static Library (.lib), but I cannot actually find the compiled .lib file in the project directory, just an .obj file.
When I try and build Project B it throws the following error message.
"LNK2019 unresolved external symbol "double __cdecl FrameTransformations::Add(double,double)" (?Add#FrameTransformations##YANNN#Z) referenced in function _main FrameTransformationTests S:\Projects\Cpp\Code\FrameTransformations\FrameTransformationTests\Main.obj 1"
I've had a look and it seems that project B can't find the Add function from project A, although I get auto-completion for the function and arguments through Intellisense (is this just because it can find the Lib1.h file ok?).
I have tried adding in the path to the Project A folder in the Project B Properties > C/C++ > General > Additional #using Directories (both to the Project folder and to the Debug folder where I expected the Lib1.lib file to be generated), but that hasn't worked.
Can someone please point me to what I've done wrong, this has had me stumped for a couple of hours now.
Thank you.
The .lib file is generally under the solution directory rather than the more specific project directory:
Look in \ or possibly \\.
When you #include a .h file, you are usually just including declarations into your code (exceptions apply, but not in this case). These are separate from definitions which are placed into .cpp files.
By default your projects do not automatically shared any of this information with each other. Your definitions nor declarations from Project A do not magically propagate to Project B, for example.
But as you've done here, you can reference declarations from Project A into Project B by #includeing header files. These, however, are just declarations! They just explain the existence of a function but don't implement it -- You need to add the definition of your Add function to Project B.
The process of bringing such definitions together is called linking. You need to link the definitions of Project A (the .lib, more precisely) into Project B. The way you do this changes with each version of Visual Studio just slightly, but it's usually under some "Linker Options" and some "Dependencies" tab/window.
You need to add reference of Project A into Project B->Project Properties as below,
Also, you need to add a reference in like below

Eclipse cdt: Includes header file correct, compiles, but highlights source code: "Unresolved inclusion"

I have a project that uses a shared library from another project.
In project settings I put the correct include paths and library for the GCC and G++ compiler (-L and -l option). It all compiles well, no problems here.
But the source code is not analyzed correctly.
My included headerfile (that is located in the other project) is marked as "Unresolved inclusion and everywhere I use something from it, the source is highlighted as well.
#include "myHeader.h"
Any ideas? thanks!
The one that you are missing here (probably) is, telling the indexer where to look for those headers.
I normally manage my own Makefile, so I do not know how to make it work for both the eclipse managed makefile and for the Indexer. Probably you will find that the solution below will fix both.
On the solution; right click on the project in the Project explorer ( or resource explorer ), and select Properties. Now under "C/C++ General" > "Paths and Symbols", click on Includes tab and select "GNU C++". Then on the right side, you can add various include paths ( similar to the -I option on gcc/g++ ) by clicking on "Add..." button.
Once you apply and click OK, the indexer will take a while to clear those unresolved object.
A header should be included like this
#include "myHeader.h"
or if it's a standard lib header:
#include <string>
everything else is invalid.
Remember to enable the Providers in the "Preprocessor Include Paths, Macros, etc.".

Include <string> not found compile error in Xcode 4.2

I'm getting include not found compile error in XCode. I have an iOS app project that i use Objective-c and c++ as mix.
Initially, i created one .h file and one .cpp file in my ios project. Then, I renamed the .cpp file to .mm file.
Here is my .h file;
TestLog.h
#ifndef CalculatorDemo_TestLog_h
#define CalculatorDemo_TestLog_h
#include <string>
using namespace std;
class TestLog
{
private:
string logString;
public:
void Log(string logMessage);
};
#endif
TestLog.mm
#include "TestLog.h"
void TestLog::Log(string logMessage)
{
//this->logString->append(logMessage);
}
What am I missing? Do I need to add std c++ library to my targetS? Something related to Search Header Paths?
I just need to use string type.
Thanks much for in advance
select project -> build setting -> apple LLVM compiler 5.1 -> language
In Compile Sources As change to Objective-C++
There's a quirk in XCode. I noticed it in 7.3. Most projects recognize .mm files and the STL, while one project I had did not. The fix was that I had to click on the top-left project icon, then click Targets > Build Phases > Link Binary with Libraries > and add in AppKit.framework. Next, I had to click Targets > Build Settings > search on "Compile Sources", and set it to "Objective C++" on all possible columns. Then, do a Clean and then a Build from the Product menu. This compiled properly then. Then, go back to that Compile Sources again and set it back to "According to File Type" on all possible columns. Then, click Build from the Product menu again. At that point, it compiled properly and allowed me to utilize the "according to file type" option, which I like better.
Oh, and if doing Cocoa stuff, don't forget to add the following header in your files:
#import <Cocoa/Cocoa.h>
And if doing command line stuff, don't forget to add the following instead of the Cocoa header:
#import <Foundation/Foundation.h>
i believe you need to include the whole path to the library. similarly to say "foundation" & "uiview" frameworks.
#import <Foundation/Foundation.h>
or
#import <UIKit/UIKit.h>
and yes, make sure you add the library to your target.
So I was having this issue with the Cocoapods library Bypass and none of these solutions did anything. The problem was with a file which Cocoapods creates called an umbrella header. This is located in <POD_NAME>/Support Files/<POD_NAME>-umbrella.h. Delete it, and it should build just fine.
Now for the explanation of why this is necessary: the umbrella header is mixing both C++ and Objective-C code directly in a header which is a big no-no apparently and ends up completely breaking the C++ imports. By removing it (which seems to have no effect?) this conflicting import which Cocoapods unknowingly created will go away.
Ran into this with xcode 12.4 with a project that is objective-c, but where I need one C++ modul. Solution: wrap the contents of the .h file in:
#if defined __cplusplus
declarations
#endif
Apparently xcode is not good at detecting a mix of sources.
see Expected ; after top level declarator, error in xcode
This often happens when Xcode doesn't understand that a C++ header file you've imported into Objective-C is actually for C++ code.
So you can solve this problem by finding the Objective-C file that imports C++ code, and simply change its extension from .m to .mm

unable to link FMOD library to VS2010 project

I am trying to link FMOD to my project, which I did very easily in the past in Visual Studio 2008.... So I have placed the fmodex_vc.lib and the fmodex.dll file in my project directory, added them to my project's solution explorer, then created a SoundMgr.h file which includes the fmod.h file
#include "include\fmod\fmod.h"
Where fmod has been placed in the include\fmod folder and opens ok if i right click on the above code and click "Open Document"...
But if I try to write any code at all, including a simple "using namespace FMOD" it tells me that it FMOD is undeclared or unidentified.... am I missing any step?
EDIT:
What the class looks like so far is:
#pragma once
#include "main.h"
#include "include\fmod\fmod.hpp"
#include "include\fmod\fmod_errors.h"
#include "include\fmod\fmod.h"
class SoundMgr{
void init();
};
void SoundMgr::init(){
FSOUND_Init (44100, 32, 0);
}
And the error is:
Error 1 error C3861: 'FSOUND_Init': identifier not found
And that's for any line of the sample code that I try import from this quick guide:
GameDev FMOD quick guide
I tried adding the library as an additional dependency in the Input section of the Properties/Linker and I get
1. fatal error LNK1181: cannot open input file 'fmodex_vc.lib'
Any of these errors ring a bell?
Don't you want fmod.hpp to get the c++ features?
you can include the headers path in C/C++ > General and library path to Linker properties and include the dll's in you project. In this case you have the files in you release/debug dir
Right so I eventually fixed it by removing the Additional Dependency in the Input section of the Linker and instead adding Include and Library directories in in Configuration Properties\VC++ directories.... Most articles I found advise to use the actual full path to the FMOD installation folder, but since I want this project to be portable and self contained, i created a "lib" and "include" folder in my project and put those files in them... (used the directories "\lib" and "\include" in the project properties which I am assuming links to the project folder, have never done this before but am hoping it won't cause dependency issues if I compile this on a different machine)...