nodepp icon indicating copy to clipboard operation
nodepp copied to clipboard

Linking errors of Nodepp while using it in multiple .cpp files in my project

Open lsergiy opened this issue 7 months ago • 3 comments

Hello dear people of Nodepp project! Including basic headers of Nodepp in two or more *.cpp files of my project produces linking errors:

duplicate symbol 'SHOULD_CLOSE()' in:
    ... ... ...
duplicate symbol 'nodepp::process::onSIGCLOSE' in:
    ... ... ...
duplicate symbol 'nodepp::process::onSIGEXIT' in:
    ... ... ...
duplicate symbol 'nodepp::process::NODEPP_EVLOOP()' in:
    ... ... ...
duplicate symbol 'nodepp::process::NODEPP_INVOKE()' in:
    ... ... ...
duplicate symbol 'nodepp::process::onSIGERROR' in:
    ... ... ...

Here are my Nodepp Includes.

    #include <nodepp/nodepp.h>
    #include <nodepp/date.h>
    #include <nodepp/http.h>
    #include <nodepp/ws.h>

Functions meant in liker output containing errors are completely defined in header files of Nodepp. They are not static and not inline. This is why multiple instance of these functions appear in my project. I don't see any tricky workaround here. I suspect some modification in Nodepp needed to make usage of it in multiple files possible. You may move these functions bodies into .cpp file but Nodepp will stop being header only library. You may move these functions into special implementation .h file and ask Nodepp users to include it only once somewhere in their projects. Anyway I trust your taste and choice on fixing this.

lsergiy avatar Apr 07 '26 13:04 lsergiy

Hello Sergiy,

I never thought this day would come so soon, but you are absolutely right. The current structure of Nodepp was violating the One Definition Rule (ODR), making it impossible to import into static libraries without linker conflicts. However, I’ve been working on this since v1.3.0, and your feedback was the final push needed.

In Nodepp v1.4.0_5, I have refactored the core to strictly obey this rule. Nodepp is now fully compatible with static libraries and multiple translation units.

Recommendations for Module Isolation

When you create a static library and import it into a project, using process::add() directly within a main function (e.g., void start_module_1()) attaches that task to the main-thread's event loop. To achieve real isolation and avoid interference between your library and the host application, I highly recommend using Workers and Channels (similar to our discussion in Issue#25):

pair_t<worker_t,channel_t<coroutine_t>> module_1_start(){
     channel_t<coroutine_t> chn;

     return { worker::add( coroutine::add( COROUTINE(){
     coBegin
          while( true ){
              if ( !chn.empty() ){
                  for ( auto &x: chn.read() ){ process::add( x ); }
              }
              if ( process::size() == 0 ){ coDelay(1000); } 
              process::next(); 
          }
     coFinish
     }) ), chn };
}

Then, from the main thread, you can inject tasks asynchronously using the channel_t:

auto module1 = module_1_start();

module1.second.write( coroutine_t( COROUTINE(){
coBegin
    while( true ){
        console::log( "Hello from isolated module 1" );
        coDelay(100); 
    }
coFinish
}));

This ensures your modules remain isolated and don't pollute the host's main reactor. I have added a full example here showing how to use static libraries as isolated modules.

I believe Nodepp is now ready for v1.4.1 or a much more stable re-release of v1.4.0. I look forward to your feedback.

Best regards,

Enmanuel D. Becerra C. Creator of Nodepp — The Doom of Async Frameworks

EDBCREPO avatar Apr 08 '26 14:04 EDBCREPO

This functionality is precious part of effective asynchronous app. I am experimenting with it. Everything works so far. I can use Nodepp in multiple C++ files also.

Thanks!

lsergiy avatar Apr 08 '26 16:04 lsergiy

Please notice, all the C++ namespace usage modifications needed to do are in range code range within [ coDelay ... onMain ]

/*────────────────────────────────────────────────────────────────────────────*/

#define coDelay(VALUE)           do { _time_=nodepp::process::millis()+VALUE; while( nodepp::process::millis()<_time_ ){ coErrno(VALUE,_LINE_,1); }} while(0);
#define coUDelay(VALUE)          do { _time_=nodepp::process::micros()+VALUE; while( nodepp::process::micros()<_time_ ){ coNext; }} while(0);
#define coErrno(DELAY,STATE,OUT) do { coSet(STATE); nodepp::coroutine::getno( OUT,coGet,DELAY ); return OUT; case STATE:; } while(0);

/*────────────────────────────────────────────────────────────────────────────*/

#define coGoto(VALUE)  do { coSet( VALUE ); nodepp::coroutine::getno(1,coGet); return 1; } while(0);
#define coStay(VALUE)  do { coSet( VALUE ); nodepp::coroutine::getno(0,coGet); return 0; } while(0);
#define coNext         do { coErrno(0UL,_LINE_,1); /*-------------------------*/ } while(0);
#define coYield(VALUE) do { coErrno(0UL, VALUE,1); /*-------------------------*/ } while(0);
#define coWait(VALUE)  do { while( VALUE ){ /*------------------------*/ coNext;}} while(0);
#define coEnd          do { _time_=0; _state_=_time_; /**/ nodepp::coroutine::getno(-1); } while(0); return -1;
#define coStop            } _time_=0; _state_=_time_; /**/ nodepp::coroutine::getno(-1); } while(0); return -1;

/*────────────────────────────────────────────────────────────────────────────*/

#define coStart  thread_local static int _state_=0; thread_local static ulong _time_=0; coBegin
#define coBegin  do { switch(_state_) { case 0:; nodepp::coroutine::getno(-2);
#define coEmit   int operator()

#define coSet(VALUE) _state_ = VALUE
#define coGet        _state_
#define coFinish     coStop

/*────────────────────────────────────────────────────────────────────────────*/

#define onMain INIT(); int main( int argc, char** args ) { \
   nodepp::process::start( argc,args ); INIT(); \
   nodepp::process::wait (); return 0;          \
}  void INIT

/*────────────────────────────────────────────────────────────────────────────*/

lsergiy avatar Apr 09 '26 14:04 lsergiy