How to export functions and classes from module not using `export` keyword? - c++

I'm investigating an opportunity of using C++ Modules TS in my pet project. One of the important use cases for me is wrapping of legacy headers.
Suppose I have a header file with some functions and classes, std.io.ixx:
int f(int x)
{
return 2 + x;
}
According to this article, I compile the module using the following command:
cl /c /experimental:module /module:name std.io /module:export std.io.ixx
Which gives me a new file std.io.ifc. Then I use this module in another source file, main.cxx:
import std.io;
int main()
{
f(5);
}
Which is compiled with the following command:
cl /c /experimental:module main.cxx
The compilation gives me a following error:
main.cxx(5): error C3861: 'f': identifier not found
So, as we can see, the identifiers from the module were not exported. I could fix this by manually adding export keyword before each identifier I want to export, but this is impossible for the use case of wrapping the legacy headers.
What I'm doing wrong? How to export all possible identifiers from a header?

I believe there are two things wrong:
The module name must not start with std.. When I attempted to do that, I got the error
error C3674: could not find standard library module 'std.io'
This error might not appear if you haven't installed the Standard Library Modules component for Visual Studio. I'm not sure about this, though.
In the blog post you linked to, there's this note:
Note that you currently have to include your header in a .cpp file (or rename your header) because of a limitation in our compiler’s file handling.
That indeed appears to be the case because when I tried with the extension ixx, I got the same error as you.
But after fixing both of the above issues, it worked fine.

Related

Why am I getting this error with MinGW and Cmake?

So I am trying to compile this flip fluids addon with blender, so I followed this tutorial, https://www.youtube.com/watch?v=TVKM1egDoGs , So I first ran cmake.exe -G "MinGW Makefiles" .. and it was telling me that it was missing a CMakeLists File... So I added one... then I ran cmake.exe --build . and it got to 3 percent than showed me errors like ...
In file included from C:/Users/N/Downloads/Blender-FLIP-Fluids-master/src/engine/threadutils.h:29,
from C:/Users/N/Downloads/Blender-FLIP-Fluids-master/src/engine/meshlevelset.h:65,
from C:/Users/N/Downloads/Blender-FLIP-Fluids-master/src/engine/meshobject.h:30,
from C:/Users/N/Downloads/Blender-FLIP-Fluids-master/src/engine/fluidsimulation.h:38,
from C:\Users\N\Downloads\Blender-FLIP-Fluids-master\src\engine\c_bindings\fluidsimulation_c.cpp:25:
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/mutex:93:9: note: previous definition of 'class std::recursive_mutex'
class recursive_mutex : private __recursive_mutex_base
^~~~~~~~~~~~~~~
In file included from C:/Users/N/Downloads/Blender-FLIP-Fluids-master/src/engine/mingw32_threads/mingw.condition_variable.h:25,
from C:/Users/N/Downloads/Blender-FLIP-Fluids-master/src/engine/threadutils.h:31,
from C:/Users/N/Downloads/Blender-FLIP-Fluids-master/src/engine/meshlevelset.h:65,
from C:/Users/N/Downloads/Blender-FLIP-Fluids-master/src/engine/meshobject.h:30,
from C:/Users/N/Downloads/Blender-FLIP-Fluids-master/src/engine/fluidsimulation.h:38,
from C:\Users\N\Downloads\Blender-FLIP-Fluids-master\src\engine\c_bindings\fluidsimulation_c.cpp:25:
C:/Users/N/Downloads/Blender-FLIP-Fluids-master/src/engine/mingw32_threads/mingw.mutex.h:142:44: error: conflicting declaration 'typedef class std::_NonRecursive<std::recursive_mutex> std::mutex'
This is the full error https://pastebin.com/KTnALG1E, where is this coming from?? is it a mingw error or what? Can someone help please?
Your full error message includes a more interesting bit:
C:/Users/N/Downloads/Blender-FLIP-Fluids-master/src/engine/mingw32_threads/mingw.thread.h:32:2:
error: #error This version of MinGW seems to include a win32 port of
pthreads, and probably already has C++11 std threading classes
implemented, based on pthreads. It is likely that you will get class
redefinition errors below, and unfortunately this implementation can
not be used standalone and independent of the system header,
since it relies on it for std::unique_lock and other utility classes.
If you would still like to use this implementation (as it is more
lightweight), you have to edit the c++-config.h system header of your
MinGW to not define _GLIBCXX_HAS_GTHREADS. This will prevent system
headers from defining actual threading classes while still defining
the necessary utility classes.
So either install a different version of mingw or edit your mingw files according to the instructions above.

Using C++ inside an iOS CocoaPod

I am building a private cocoapod for iOS, and am running into issues with some C++ code. The project builds fine in XCode, but when I attempt to run pod lib lint MyProjectName.podspec I get the following error:
- ERROR | xcodebuild: /path/to/aheader.h:2:10: error: 'string' file not found
The header has the following first line:
#include <string>
Searching for possible solutions, I added the following to podspec (based on CocoaPods: Linking with C++ symbols defined in libPods.a)
s.source_files = "MyProjectName/**/*.{swift,c,m,h,mm,cpp,plist}"
s.library = 'c++'
s.xcconfig = {
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++11',
'CLANG_CXX_LIBRARY' => 'libc++'
}
But it made no difference to the error. Another suggestion I saw was to "use a wrapper", but this piece of code (which is 3rd-party IP that I can not port to Objective C) is already using a wrapper.
How can I build the pod successfully by mixing both Objective C and C++ along with Swift? Any (non-null) pointers would be appreciated.
I had to simply renamed the C++ header extension to .hpp, so it was not included by default in the source_files filter. This resolved the issue, as the wrapper was including the header. Posting this in case someone else runs into the same issue.
Update: Nope, it just lets the app build, but using the pod still doesn't work.
Try explicitly adding stdc++ in the linker options.
s.pod_target_xcconfig = {
'OTHER_LDFLAGS' => '-l"stdc++"'
}

Building OpenSSL under Borland C++ Builder, Win 8.1

I'm following the instructions from INSTALL.W32 (various OpenSSL versions), in general it's:
* Configure for building with Borland Builder:
> perl Configure BC-32
* Create the appropriate makefile
> ms\do_nasm
* Build
> make -f ms\bcb.mak
Now, I do get two kind of errors when trying to build:
For OpenSSL < 1.0.0
nasmw -f obj -d__omf__ -ocrypto\md5\asm\m5_win32.obj .\crypto\md5\asm\m5_win32.asm
'nasmw' is not recognized as an internal or external command,
operable program or batch file.
Otherwise
Warning W8017 C:\CBuilder5\Include\sys/stat.h 34: Redefinition of 'S_IFMT' is not identical
Warning W8017 C:\CBuilder5\Include\sys/stat.h 35: Redefinition of 'S_IFDIR' is not identical
Error E2227 .\crypto\rand\randfile.c 226: Extra parameter in call to _open in function RAND_write_file
Warning W8053 .\crypto\rand\randfile.c 262: '_chmod(const signed char *,int,...)' is obsolete in function RAND_write_file
*** 1 errors in Compile ***
Yes, I feel bad for using Borland C++ Builder 5 but I can't do anything about it, and yes, I consider the Shining Light option if everything else fails.
I do not use OpenSSL but from your text some hints:
NASM
is not default assembler for Borland compilers
you need to download and install it first
(it is for free and one of the best compilers I used in the past)
you are missing some #define before including OpenSSL or including in wrong order
that is why you have the first warnings and most likely also the error
some libs need to add configuration #defines (added by some specific IDE's)
to specify what compiler,platform,endianess... is used
before any includes
usually if you include in wrong order then the defines are defined for some instances of a lib file but not for all
so try to reorder the includes
sometimes helps to include before the lib some other thing like conio,stdio,windows,...
to determine which define is missing or wrong open the stat.h
and look for #ifdef #ifndef statements around S_IFMT
The solution for the newest version (1.0.2d) was to:
remove the extra parameter from randfile (which, as comment stated, was unnecessary),
edit ms\bcb.mak, search for -DMD5_ASM -DSHA1_ASM -DRMD160_ASM
and change to -DMD5_NO_ASM -DSHA1_NO_ASM -DRMD160_NO_ASM . (there was an unresolved external error with SHA, MD5 and RMD160, they basically couldn't be compiled in asm).
There were some other steps included as well, but they're just specific to my environment.

Member declaration not found

I have worked on a C++ project using a regular text editor. Later, I imported all the files to Eclipse to make it debugging easier.
In Eclipse a weird thing happens. It complains "Member declaration not found" even if I have included the header file. The header file has the function definition.
How do I fix this problem?
"Member declaration not found" is an error produced by the Eclipse static analysis tool (codan). If you get this error, but the compilation succeeds this is a false positive. Older versions of this tool are known to give some false positives, see for example this bug report. So I recommend updating Eclipse CDT to the most recent version.
Another thing that may cause this error is an unresolved include that prevents Eclipse from parsing a portion of your code correctly. Selecting Index -> Search For Unresolved Includes in the context menu of the project will give you the list of unresolved includes. See this answer for the details of how to fix it.
Here's an example:
class C {
void f(std::vector<int>&);
};
void C::f(std::vector<int>&) {} // Member declaration not found
The above example causes "Member declaration not found" error in Eclipse CDT even if you have <vector> included but unresolved (due to misconfigured include paths).
I also experienced this problem several times in Eclipse though building is successful. We can simply solve this problem by rebuild the C/C++ index in project menu. :)
I got this problem in Eclipse, but building in terminal was successful. So I just rebuild the C/C++ index in Eclipse:
Right click on the project -> index -> rebuild.
I noticed that "Member declaration not found" will report also when you create a class with a name that is already used or is a a keyword.
I found an error in my .cpp file that creates this message. I had namespace std { in the front of the file, and I placed new functions that I was creating after the closing } for namespace. Moving the closing } to the end of the file so that the defined files were now in the namespace fixed the error message.
Example that creates the error.
#include "MyStrFuncs.h"
**namespace** std {
MyStrFuncs::MyStrFuncs() {
// TODO Auto-generated constructor stub
}
MyStrFuncs::~MyStrFuncs() {
// TODO Auto-generated destructor stub
}
} // This ends the **namespace**
//Additional functions will now generate the member declaration not found error...
int MyStrFuncs::str2i(string strIn) {
int results;
istringstream convert(strIn);
if( !(convert)>>results) results = 0;
return results;
}
// Fix by moving closing } for namespace to here. Good luck.
Even with the CDT 9.2.1 and Eclipse Neon 4.6.3 "Member declaration not found" problems are reported.
As answered by Srijeyanthan, the following should resolve it:
Project > C/C++ Index > Rebuild.
I also experienced this problem while splitting source and header files in eclipse.I resolved this by "implementing methods" eclipse instead of manual typing and building the project.By implementing methods "inline functions" will be added to source file.

Strange error when adding #include <string>

I have the following very simple application that compiles and runs fine:
EDIT: changed the example to be simpilar to end confusion of the real issue
int main() {
return 0;
}
As soon as I add #include <string> (and not even reference std::string), it fails to compile and I get the following error:
/usr/include/c++/4.1.2/bits/allocator.h:82 error: expected template-name before '<' token
Along with about 456 other, similar errors.
Any ideas? Thanks!
UPDATE:
Line 82 of /usr/include/c++/4.1.2/bits/allocator.h references the template __glibcxx_base_allocator at the location of the error. That template is defined in bits/c++allocator.h. When I search the system for that file, I get 3 hits, but none of them are in /usr/include/c++/4.1.2/bits/ as one would expect.
I have version 3.1.6, 4.1.1, and 4.3.2, but not 4.1.2 as the rest of the includes I am using. I am not sure which one is being used (if any, however, I don't get any error for an unknown file), but it seems the problem may stem from this.
The problem appears to be the installed development packages are not correct or incomplete (not to be confused with corrupt). Forcing g++ to use different include versions corrects that:
g++ -nostdic++ hello.cc -o hello -I/usr/include/c++/3.4.6
All the alternative directories (4.1.1, 4.1.2 and 4.3.2) are incomplete causing inappropriate files to be included causing the unusually errors. For example:
/usr/include/c++/4.1.2/bits/allocator.h requires __glibcxx_base_allocator located in bits/c++allocator.h which is being included from either /usr/include/c++/4.1.1 or /usr/include/c++/4.3.2 and appear to be incompatible. Forcing the compiler to use the only complete set of includes rectifies this.
Almost certainly g++ is detecting .cc as a C source file, not C++ and passes it through to gcc instead of compiling as C++. You can easily test by renaming your file to hello.C. There's also a language parameter to g++ you can use.
EDIT: This seems to work fine in g++ 4.2 with a .cc extension so that might not be it. Do you have any other headers included you aren't showing us? They could be interfering with <string>.
EDIT2: Alternatively your headers might not be set up right. Does this work:
#include <string>
int main()
{
return 0;
}
Errors like this have been heard of to occur when the C++ standard library headers are corrupted/not fully installed – maybe there is even a message referring to a missing include among your 456 other errors.
In any case, make sure that libstdc++-devel, resp. the package containing the C++ standard library header files of your distribution, is properly installed.
Check your include path. The paths can be specified as environment variables or specified on the command line. You could be using an include file from a different compiler or different version of the same compiler.
Also, try using <cstdio> rather than <stdio.h>.
Another suggestion: change <> to "".
This could be error caused at preprocess stage. Just preprocess your cpp file by passing flag -E to gcc and Look at the place the compiler complains.