Results 1 to 3 of 3

Thread: new problem with simple C++ static variables

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2008
    Posts
    38
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    19

    Default new problem with simple C++ static variables

    I moved some stuff around in my TCP/IP program and now I get a duplicate defined
    message for static variables.

    The strange thing is that the compiler is calling the use of a static variable a definition. :confused:

    Qt Code:
    1. #ifndef TCPIP_SERVER_H_
    2. #define TCPIP_SERVER_H_
    3. #include "ui_tcpip.h"
    4. #include "inet.h"
    5. #include <iostream>
    6. #include <sstream>
    7. #include <exception>
    8. using namespace std;
    9. #include "thread.cpp"
    10. #include "sock_io.cpp"
    11. #include "tstash.h"
    12. #include <QtDebug>
    13.  
    14. typedef void* (*p_VOID_FUNCT)(void*);
    15. class tcpip_server {
    16.  
    17. socklen_t clilen;
    18. struct sockaddr_in cli_addr, serv_addr;
    19. ushort port;
    20. // struct sock_addr_in accept_ip_struct;
    21. int accept_ip_address;
    22. int client_ip_address;
    23. Ui::tcpipClass ui;
    24. public:
    25. tcpip_server(int accept_ip_addr,
    26. ushort port_port,
    27. p_VOID_FUNCT p_void_funct, Ui::tcpipClass uip);
    28. //:
    29. // accept_ip_address(accept_ip_addr),
    30. // port(port_port),
    31. // ui(uip);
    32. // destructor
    33. ~tcpip_server()
    34. {
    35. close(socketfd); /* close original socket */
    36. }
    37. static int socketfd, newsocketfd;
    38. static QList<QString> qlist;
    39. static bool running;
    40. static QStringList qstrList;
    41. static void localClose()
    42. {
    43. qDebug()<<"close detected";
    44. running = false;
    45. close(socketfd); // obviously this is NOT a definition
    46. close(newsocketfd);
    47. exit(0);
    48. }
    49. static void* str_echo(void* p_void);
    50. // struct sock_addr_in client_ip_struct;
    51.  
    52.  
    53. };
    54. bool tcpip_server::running = true;
    55. int tcpip_server::socketfd = 0;
    56. int tcpip_server::newsocketfd = 0;
    57. QStringList tcpip_server::qstrList = QStringList();
    58. QList<QString> tcpip_server::qlist = QList<QString>();
    59. #endif /*TCPIP_SERVER_H_*/
    To copy to clipboard, switch view to plain text mode 

    link errors for each static variable!

    make release
    make -f Makefile.Release
    make[1]: Entering directory `/home/landon4/workspace/tcpip'
    g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include/QtGui -I/usr/include -Irelease -I. -o release/tcpip_client.o tcpip_client.cpp
    g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include/QtGui -I/usr/include -Irelease -I. -o release/tcpip_server.o tcpip_server.cpp
    tcpip_client.h: In constructor ‘tcpip_client::tcpip_client(int, ushort, bool, Ui::tcpipClass)’:
    tcpip_client.h:14: warning: ‘tcpip_client::accept_ip_address’ will be initialized after
    tcpip_client.h:12: warning: ‘ushort tcpip_client::port’
    tcpip_client.cpp:17: warning: when initialized here
    tcpip_client.cpp: At global scope:
    tcpip_client.cpp:17: warning: unused parameter ‘ui’
    tcpip_client.cpp: In member function ‘int tcpip_client::tcp_write(char*, int)’:
    tcpip_client.cpp:64: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result
    g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include/QtGui -I/usr/include -Irelease -I. -o release/main.o main.cpp
    In file included from tcpip_server.h:11,
    from tcpip_server.cpp:2:
    sock_io.cpp: In member function ‘int sock_io::tcp_write(char*, int)’:
    sock_io.cpp:18: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result
    tcpip_server.h: In constructor ‘tcpip_server::tcpip_server(int, ushort, void* (*)(void*), Ui::tcpipClass)’:
    tcpip_server.h:21: warning: ‘tcpip_server::accept_ip_address’ will be initialized after
    tcpip_server.h:19: warning: ‘ushort tcpip_server::port’
    tcpip_server.cpp:4: warning: when initialized here
    tcpip_server.cpp:43: warning: suggest parentheses around assignment used as truth value
    g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include/QtGui -I/usr/include -Irelease -I. -o release/tcpip.o tcpip.cpp
    /usr/lib/qt4/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include/QtGui -I/usr/include -Irelease -I. tcpip.h -o release/moc_tcpip.cpp
    In file included from tcpip_server.h:11,
    from tcpip.cpp:8:
    sock_io.cpp: In member function ‘int sock_io::tcp_write(char*, int)’:
    sock_io.cpp:18: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result
    g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include/QtGui -I/usr/include -Irelease -I. -o release/moc_tcpip.o release/moc_tcpip.cpp
    g++ -o tcpip release/tcpip_client.o release/tcpip_server.o release/main.o release/tcpip.o release/moc_tcpip.o -lQtGui -lQtCore -lpthread
    make[1]: Leaving directory `/home/landon4/workspace/tcpip'
    release/tcpip.o:/usr/include/QtCore/qlist.h:527: multiple definition of `tcpip_server::qstrList'
    release/tcpip_server.o:/usr/include/QtCore/qatomic_i386.h:90: first defined here
    release/tcpip.o:/usr/include/QtCore/qlist.h:559: multiple definition of `tcpip_server::qlist'
    release/tcpip_server.o:/usr/lib/gcc/i386-redhat-linux/4.3.0/../../../../include/c++/4.3.0/iostream:77: first defined here
    release/tcpip.o:/home/landon4/workspace/tcpip/tcpip.cpp:175: multiple definition of `tcpip_server::socketfd'
    release/tcpip_server.o:/home/landon4/workspace/tcpip/tcpip_server.cpp:90: first defined here
    release/tcpip.o:(.data+0x0): multiple definition of `tcpip_server::running'
    release/tcpip_server.o:(.data+0x0): first defined here
    release/tcpip.o:/home/landon4/workspace/tcpip/tcpip.cpp:175: multiple definition of `tcpip_server::newsocketfd'
    release/tcpip_server.o:/usr/include/QtCore/qatomic_i386.h:90: first defined here
    collect2: ld returned 1 exit status
    make[1]: *** [tcpip] Error 1
    make: *** [release] Error 2
    Last edited by jacek; 17th August 2008 at 02:36. Reason: changed [code] to [quote]

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. problem with static building
    By mismael85 in forum Qt Programming
    Replies: 11
    Last Post: 28th March 2008, 20:43
  3. Problem emitting signal from a static function
    By Valheru in forum Qt Programming
    Replies: 21
    Last Post: 12th June 2007, 14:48
  4. problem after Qt 4.2.2 static build
    By npc in forum Newbie
    Replies: 2
    Last Post: 23rd February 2007, 12:28
  5. Problem with libmng on static link
    By jlbrd in forum Installation and Deployment
    Replies: 2
    Last Post: 10th May 2006, 22:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.