Author Topic: must add a file to my project [SOLVED]  (Read 3257 times)

Offline bbd

  • Single posting newcomer
  • *
  • Posts: 4
must add a file to my project [SOLVED]
« on: October 16, 2019, 02:21:17 pm »
Hello friends.
Im coping with a strange issue. I've built a project with several libraries. One of them is composed of 2 files let's say toto.h and toto.cpp. I must add the file toto.cpp to my project to make the project build correctly. If not it raises an error about unedfined reference about the arguments of a function defined in toto.cpp. Obviously there was an include pointing to the file and search paths set correctly.
For the other lib I don't need to add the files to my project . the includes are enough.
« Last Edit: October 16, 2019, 05:33:18 pm by bbd »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1557
Re: muts add a file to my project
« Reply #1 on: October 16, 2019, 02:31:13 pm »
You must compile the library separately and then link with the generated toto.a.

When reporting compilation problems you must always attach a full rebuild log in code tags, see:
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F

Offline bbd

  • Single posting newcomer
  • *
  • Posts: 4
Re: muts add a file to my project
« Reply #2 on: October 16, 2019, 02:56:32 pm »
thks

In my project I must include a source file named shader.cpp/shader.hpp

I added :
#include <shader.hpp>

when I compile, the file is found but an error is raised :

||=== Build: Debug in tuto5 (compiler: GNU GCC Compiler) ===|
C:\msys64\mingw64\bin\..\lib\gcc\x86_64-w64-mingw32\9.2.0\..\..\..\..\x86_64-w64-mingw32\bin\ld.exe: obj\Debug\tutorial05_textured_cube\tutorial05.o||in function `main':|
C:\Users\.......\Desktop\ogl-master\tutorial05_textured_cube\tutorial05.cpp|67|undefined reference to `LoadShaders(char const*, char const*)'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

the call to the function LoadShaders is correct in my main as it is :
GLuint programID = LoadShaders( "TransformVertexShader.vertexshader", "TextureFragmentShader.fragmentshader" );

I just need to add the file shader.cpp to my project in the managment window and it compiles correctly.

Can you explain me what was going on please ?
« Last Edit: October 16, 2019, 03:20:32 pm by bbd »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1557
Re: muts add a file to my project
« Reply #3 on: October 16, 2019, 04:31:50 pm »
This is not a full rebuild log, and it does not show the compiler command line. Also, you have not used code tags. Have you read the link I gave you?

You must add the code in shader.cpp to the project using one of these methods:
  - just adding it to the project. As you have seen, this works.
  - creating a library contaning the code in shader.cpp (say shader.a) and linking it to your code using the project's linker options

Offline bbd

  • Single posting newcomer
  • *
  • Posts: 4
Re: muts add a file to my project
« Reply #4 on: October 16, 2019, 04:53:17 pm »
Code
 Clean: Debug in tuto7 (compiler: GNU GCC Compiler)---------------

Cleaned "tuto7 - Debug"

-------------- Build: Debug in tuto7 (compiler: GNU GCC Compiler)---------------

g++.exe -Wall -g -IC:\Users\\Desktop\ogl-master\common -IC:\Users\\Desktop\ogl-master\external\glm-0.9.7.1 -IC:\msys64\mingw64\include\GL -IC:\msys64\mingw64\include -IC:\newton-dynamics-master\sdk\dgNewton -c C:\Users\\Desktop\ogl-master\common\controls.cpp -o obj\Debug\common\controls.o
g++.exe -Wall -g -IC:\Users\\Desktop\ogl-master\common -IC:\Users\\Desktop\ogl-master\external\glm-0.9.7.1 -IC:\msys64\mingw64\include\GL -IC:\msys64\mingw64\include -IC:\newton-dynamics-master\sdk\dgNewton -c C:\Users\\Desktop\ogl-master\common\objloader.cpp -o obj\Debug\common\objloader.o
g++.exe -Wall -g -IC:\Users\\Desktop\ogl-master\common -IC:\Users\\Desktop\ogl-master\external\glm-0.9.7.1 -IC:\msys64\mingw64\include\GL -IC:\msys64\mingw64\include -IC:\newton-dynamics-master\sdk\dgNewton -c C:\Users\\Desktop\ogl-master\common\texture.cpp -o obj\Debug\common\texture.o
g++.exe -Wall -g -IC:\Users\\Desktop\ogl-master\common -IC:\Users\\Desktop\ogl-master\external\glm-0.9.7.1 -IC:\msys64\mingw64\include\GL -IC:\msys64\mingw64\include -IC:\newton-dynamics-master\sdk\dgNewton -c C:\Users\\Desktop\ogl-master\tutorial07_model_loading\tutorial07.cpp -o obj\Debug\tutorial07.o
g++.exe -LC:\msys64\mingw64\lib -o bin\Debug\tuto7.exe obj\Debug\common\controls.o obj\Debug\common\objloader.o obj\Debug\common\texture.o obj\Debug\tutorial07.o  -lOpenGL32 -lglu32 -lfreeglut -lglew32 -lglew32mx -lglfw3dll  C:\msys64\mingw64\lib\libfreeglut_static.a C:\msys64\mingw64\lib\libfreeglut.a C:\msys64\mingw64\lib\libglfw3.a
C:\Users\\Desktop\ogl-master\common\texture.cpp: In function 'GLuint loadDDS(const char*)':
C:\Users\\Desktop\ogl-master\common\texture.cpp:167:15: warning: unused variable 'components' [-Wunused-variable]
  167 |  unsigned int components  = (fourCC == FOURCC_DXT1) ? 3 : 4;
      |               ^~~~~~~~~~
C:\Users\\Desktop\ogl-master\tutorial07_model_loading\tutorial07.cpp: In function 'int main()':
C:\Users\\Desktop\ogl-master\tutorial07_model_loading\tutorial07.cpp:98:7: warning: unused variable 'res' [-Wunused-variable]
   98 |  bool res = loadOBJ("cube.obj", vertices, uvs, normals);
      |       ^~~
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj\Debug\tutorial07.o: in function `main':
C:/Users//Desktop/ogl-master/tutorial07_model_loading/tutorial07.cpp:83: undefined reference to `LoadShaders(char const*, char const*)'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
2 error(s), 2 warning(s) (0 minute(s), 1 second(s))
 

I'm not buiding a library just trying to include a file. I Believed adding the instruction #include and set a path to the file was enough. That is not enough ?

In my main I have several includes. For instance :

Code
...
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <shader.hpp>
...

for example I don't need to add the file glm.cpp in my project unlike shader.cpp
« Last Edit: October 16, 2019, 05:08:42 pm by bbd »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1557
Re: muts add a file to my project
« Reply #5 on: October 16, 2019, 05:31:33 pm »
Quote
I Believed adding the instruction #include and set a path to the file was enough. That is not enough ?

The include just informs the compiler about the existence of some functions and how they expect to be called. The functions themselves are in the cpp file, so you must attach (link) the code containing the functions to your project using one of the two methods I wrote before.

Quote
I don't need to add the file glm.cpp in my project unlike shader.cpp

You don't need glm.cpp probably because you are not using the functions it contains, or they are already included in one of the libraries you are linking:

Code
-lOpenGL32 -lglu32 -lfreeglut -lglew32 -lglew32mx -lglfw3dll  C:\msys64\mingw64\lib\libfreeglut_static.a C:\msys64\mingw64\lib\libfreeglut.a C:\msys64\mingw64\lib\libglfw3.a

Offline bbd

  • Single posting newcomer
  • *
  • Posts: 4
Re: muts add a file to my project
« Reply #6 on: October 16, 2019, 05:32:51 pm »
Thks Miguel
Crystal clear.