11

I am using Qt and QTcpSocket to create a small chat client/server application.

When I compile my code, the compiler returns the following error:

main.cpp:3:22: fatal error: QTcpSocket: No such file or directory

This refers to #include <QTcpSocket>.

Where is this library located and how can I install it?

Béatrice Moissinac
  • 934
  • 2
  • 16
  • 41

3 Answers3

28

You need

QT += network

in your .pro file. Otherwise the module in which those headers are defined/libraries are built won't be loaded.

Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
  • Indeed, you are right. I still get main.cpp:3:22: fatal error: QTcpSocket: No such file or directory. Ah! That's funny! So I modify my .pro, save, and run qmake -project, qmake and make in my terminal, and it changes my .pro back to before! – Béatrice Moissinac Nov 05 '12 at 04:14
  • I don't have this problem if I compile inside Qt Creator. Closing and restarting it does not affect this issue. Why are my changes not accepted by qmake -project? – Béatrice Moissinac Nov 05 '12 at 04:20
  • @Bibi541 What? .pro files aren't auto-generated in any way that I've ever heard. Something isn't right--you didn't forget to save changes or something simple like that did you? – Matt Phillips Nov 05 '12 at 04:21
  • What is different when I compile it with Qt Creator? Is it possible that the Makefile does not get updated? – Béatrice Moissinac Nov 05 '12 at 04:34
  • @Bibi541 I almost always use QtCreator, but when I have compiled/run qmake from the command line, I've never seen what you just described. I think running qmake should *always* generate a new makefile--that's the point right? Also auto-generated are the `.pro.user` files, you didn't get mixed up with one of those somehow did you? – Matt Phillips Nov 05 '12 at 04:56
  • No, because I modified my .pro in Qt Creator, which only shows the .pro in the project. There is no way I changed .pro.user instead. Strange... – Béatrice Moissinac Nov 05 '12 at 05:06
  • The only reason why I was typing the command line is because somehow my images where not loading when running it with QtCreator, but loading when doing it by hand. Well, I will compile with QtCreator for now, and if I need to load my images, I guess I will come back to stackoverflow and ask :) – Béatrice Moissinac Nov 05 '12 at 05:15
  • @Bibi541 Yes these kinds of issues with Qt pop up from time to time and can be pretty annoying. Good luck! – Matt Phillips Nov 05 '12 at 06:08
0

As I faced the problem and the obvious solution proposed by the documentation and Matt Phillips seemed not to work, I would add to check ALL your .pro files of all your potential subprojects.

As far I'm concerned, addind QT *= network did not work in the main .pro and had to be put in the one actually using the QTcpSocket and QTcpServer objects.

A. Ocannaille
  • 306
  • 4
  • 14
0

CMake

add to CMakeList.txt

find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(yourtarget PRIVATE Qt6::Network)

qmake

QT += network
Jinxuan
  • 1
  • 1