I got Errors in C++ when try to use modules

Hi

I have a python script that read IMU data from the sensor. This is the part of the scrip which get the IMU sensor data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import Microcontroller_Manager_Serial as Serial

## Function which get measurement from the IMU sensor
def IMU_Get_Values(communication_mode,choice):
    data_out = [83, 58, choice, 58, 13, 10]
    data_in = [48]
    current_data_size = 30
    current_time_out = 0.5
    if (communication_mode == 2):
       Bluetooth.Bluetooth_Send_Data(bytes(data_out))
       data_in = Bluetooth.Bluetooth_Receive_Data(current_data_size,current_time_out)
    else:
       Serial.Serial_Port_Send_Data(data_out)
       data_in = Serial.Serial_Port_Receive_Data(current_data_size,current_time_out)
    return data_in


Now from that python Script I need a C++ code that can ask for IMU data update and publish it. This is my C++ code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <stdio.h>
#include <string.h>
#include <wiringSerial.h>

export module Microcontroller_Manager_Serial.Serial;

//import Microcontroller_Manager_Bluetooth as Bluetooth

//Communication_Mode_ = 0
int Communication_Mode_ = 0;
double data_recieved=0;

int main()
{

int serial_port = serialOpen ("/dev/serial0", 115200);
if (serial_port > 0 ) {

       //double data_recieved;
data_recieved = IMU.IMU_Get_Values(1, 1)
print(colored("Raw data received: ", 'red'), data_received)

}
}


But when compile it I got the following errors:

get_data_test.cpp:21:41: warning: multi-character character constant [-Wmultichar]
print(colored("Raw data received: ", 'red'), data_received)
^~~~~
get_data_test.cpp:5:1: warning: keyword ‘export’ not implemented, and will be ignored
export module Microcontroller_Manager_Serial.Serial;
^~~~~~
get_data_test.cpp:5:8: error: ‘module’ does not name a type; did you mean ‘double’?
export module Microcontroller_Manager_Serial.Serial;
^~~~~~
double
get_data_test.cpp: In function ‘int main()’:
get_data_test.cpp:20:20: error: ‘IMU’ was not declared in this scope
data_recieved = IMU.IMU_Get_Values(1, 1)

Any help?
Last edited on
modules are new and not fully supported everywhere. Try doing it with header files and such for now.
so it will be #include < Microcontroller_Manager_Serial.h> or #include "Microcontroller_Manager_Serial.h"

And for the second error get_data_test.cpp:20:20: error: ‘IMU’ was not declared in this scope?

Or when I include it with the header file that error should disappear as well?
The only compiler known to support modules at this time is Visual Studio.
https://en.cppreference.com/w/cpp/compiler_support/20
depending on how you set up your project you may need "" instead of <> on the headers.
if you jacked the library path into the compilers search path, <> will work. If you did not, "" and exact pathing is needed.
but yes, if you include what you need, and link the library, it should work. You need to do both (include headers and link the compiled parts). This is a good skill to have anyway: it will be years before this is not used in favor of modules, maybe even decades. Getting a library to work the first few times is always a struggle for new coders in c++, you will need to read a few web pages if you have never done this.
Last edited on
Modules in C++ aren't Python modules. They may share common features.

Doing a 'net search I found some links that show how to interface Python with C; using C++ modules looks to be a red herring for what you seem to want to do.

Building a Python C Extension Module - Real Python
https://realpython.com/build-python-c-extension-module/

If C can interface with Python modules C++ sure can.

Here's the DDG metasearch I used, you might find other links that are useful to you:
https://duckduckgo.com/?q=c%2B%2B+using+python+modules&t=ffsb&ia=web

FYI, here's a simple C++ module example:

Module interface file (math.cppm):
1
2
3
4
5
6
7
8
9
10
11
12
export module math;

export auto square(const auto& x) { return x * x; }

export const double lambda { 1.303577269034296391257 };

export enum class Oddity { Even, Odd };

// module-local function (not exported)
bool isOdd(int x) { return x % 2 != 0; }

export auto getOddity(int x) { return isOdd(x) ? Oddity::Odd : Oddity::Even; }

main_modules.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import <iostream>;
import <format>;

import math;

int main()
{
   std::cout << "Lambda squared: " << square(lambda) << std::endl;

   int number;
   std::cout << "\nPlease enter an odd number: ";
   std::cin >> number;
   std::cout << std::endl;

   // if (isOdd(number))            /* Error: identifier not found: 'isOdd' */
   //   std::cout << "Well done!" << std::endl;

   switch (getOddity(number))
   {
      using enum Oddity;

   case Odd:
      std::cout << "Well done! And remember: you have to be odd to be number one!";
      break;

   case Even:
      std::cout << std::format("Odd, {} seems to be even?", number);
      break;
   }

   std::cout << std::endl;
}
Lambda squared: 1.69931

Please enter an odd number: 5

Well done! And remember: you have to be odd to be number one!

Why are lines 15-16 commented out? The isOdd function isn't exported, it is local to the math.cppm module interface file.

The same idea of having a function in one .cpp file NOT be referenced in a header file used by another .cpp source file. The 2nd .cpp source won't find the function's definition.
Excellent constant in your example :)
Last edited on
The really weird thing about that constant in the module interface file is VS whinges about it:
E3309 an export declaration cannot export a name with internal linkage

though when compiling not a peep.

Intellisense module support is still experimental, so it does report as an error what isn't one.

I'll take a report of an error that isn't with VS module support vs. all the other compilers I've tested that don't have a clue what modules are yet.

For that matter at least one C++20 library header, <format>, doesn't exist with any of the non-MS compilers I have. TDM-GCC, MinGW or whatnot. They are (hopefully) as up-to-date as they can be. Try to import/#include <format>....the compiler/pre-processor can't find the header!
This has the current up-to-date conformance level for the major compilers
https://en.cppreference.com/w/cpp/compiler_support

std::format is implemented in VS 2019 and partially implemented in Clang 14.

To use gcc and clang compilers on-line, have a look at wandbox.com. You can select which library/compiler to use.

NOTE The on-line compiler is wandbox.org and NOT wandbox.com. See following comments.

Last edited on
wandbox.com domain is for sale now.
Interesting - although as of this post it still works.
Compiler explorer:

https://godbolt.org/

Can also select which compiler, it has, g++, clang++ and msvc.latest to name a few.
Last edited on
The online compiler is wandbox.org, not .com.
Thanks George P
I got a bit worried that wandbox was gone, several of my "learn C++" eBooks use wandbox for code samples in the text. Makes it easy to read the book on my tablet and test the code with the tablet's browser

While manually entering wandbox.com into my desktop main browser I saw I had previous links to wandbox.org. Huzzah! It ain't gone! :)
The online compiler is wandbox.org, not .com.


Whoops! Even after I tried it I still didn't spot my problem. Dohh...

[Previous post edited re this]
And make sure you use the HTTPS version because the HTTP version (that I get to if I just type wandbox.org into the address bar and press enter) only shows a "Welcome to nginx!" message.
Last edited on
@Peter87, that is good advice!

Though every browser I use, desktop or tablet, slurps over to https automatically.
Topic archived. No new replies allowed.